-1

I am trying to read a file which is located at the same source as my .c code. I tried this little sketch and same problem. What could be wrong? File's extension and name are just the same.

#include <stdlib.h>
#include <stdio.h>

int main (){
    char caracter;
    FILE *ARCHIVO;
    ARCHIVO=fopen("prueba.txt", "r");
    if (ARCHIVO==NULL){
        printf("Error·");
    }else{
        while (feof(ARCHIVO)==0){
            caracter=fgetc(ARCHIVO);
            printf("%c", caracter);
        }
    }
    fclose(ARCHIVO);
    return 0;
}

I will be very thankful if someone could help me. Regards.

  • What error are you getting? – Sanjay-sopho Dec 08 '16 at 18:43
  • 1
    And avoid that use of feof. See http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Sanjay-sopho Dec 08 '16 at 18:48
  • 3
    Instead of printf("Error.") write perror("prueba.txt") to get a better error message. – AndersK Dec 08 '16 at 18:48
  • If you try a simple test such as `ARCHIVO=fopen("testprueba.txt", "w"); fclose(ARCHIVO);` is the file created at the location you expect? One thing you can do is to give `main` its full signature `int main(int argc, char *argv[])` and work out a folder name from `argv[0]` (the executable). – Weather Vane Dec 08 '16 at 18:49
  • That seems to work. At least it doesn't show the error message. Thank you very much!! – Juanjo Daza Dec 08 '16 at 18:51

1 Answers1

1

Your file doesn't have the read permission thats why it is giving the error while.

Try the following :

chmod a+r prueba.txt 

This will fix the problem. Hope so !!!

Sohil Omer
  • 1,171
  • 7
  • 14