Se puede abrir pero no se puede leer el archivo

I am trying to open a file using file = fopen("filename.txt") and read that file character by character using fgetc(file).

Opening the file is successful, but fgetc() always returns -1. To find the error I tried to use ferror(), which was true after using fgetc. But that still doesn't give any information on why the error occurs. So I was trying to use strerror(errno) to obtain details about the occurring error, but this returns "No Error". Does anyone have any idea what the error could be?

Aquí está la parte relevante del código:

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

FILE* file1;
int c;

....
void main(void) {
    file1 = fopen("C:\\Users\\student\\text1.txt", "r");
    if (NULL == file1) {
        send_string("Error opening file!"); 
    } else {
        send_string("File opened successfully\n");
        c = fgetc(file1);
        if (feof(file1))
            send_string("End of file reached"); 
        else if (ferror(file1))
            send_string("Error in file!");
        send_string(strerror(errno));   //output is "No Error!"
    }
}

El resultado es:

"Error in file!"
"No Error!"

The file I am trying to open is a simple text file containing some lines of random characters. I also tried to open a .csv file with the same result.

preguntado el 12 de junio de 14 a las 10:06

Are you certain about the output you provided? "Error on file!" isn't something that your program prints out. -

Sorry, that was a spelling mistake, the output is: "Error in file!". -

¿Por qué hay C# ¿En el título? -

¿Qué c ¿Contiene? -

Because the code is written in c, I thought it would make sense to put that in the title -

1 Respuestas

please try open file with "rb", that's because of you are using Windows!

Respondido el 12 de junio de 14 a las 11:06

Opening the file with "rb" gave the same result, unfortunately. - user3733416

¿Qué compilador estás usando? - coderonthemount

i don't have this IDE, can't move forward. Actually, your code is OK, it works fine in ubuntu. - coderonthemount

In fact the Code Composer Studio was the problem. The program is supposed to work on a microcontroller and thus has no access to the Data system of the Computer. For that reason the file cannot be read properly. Thanks for your help. - user3733416

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.