- 1
I tried to get the file's content and put it into a string.When I print the string I get the file's content with some weird char
#include<stdio.h> #include<stdlib.h> void prenom(File *f){ char c[20]; f=fopen("exo1.txt","r"); fgets(c,20,f); puts(c); fclose(f); } int main(){ File *ptr; prenom(ptr); return 0; }
10 Answers
+ 1
Ipang Wait finnaly I tried other old file I changed the encoding into ANSI and know it worked without seeing other weird chars thank u a lot.
+ 1
/****Try this ****/
void prenom(file *f) {
char *s;
f=fopen("exo1.txt", "r");
if(f==NULL)
print(" error");
else{
fscanf(f,"%s",s);
printf("%s",s);
}
fclose(f);
}
0
Hard to say cause none of us here knows the details, for example, which encoding was used when creating and saving the file exo1.txt.
I would rather suggest you to try to create another text file, and read it, to see whether or not a similar symptom occur.
Just wondered why you create the file pointer in the main() but the reading of the file is in the prenom(). Maybe it's better to create the file pointer in the prenom() instead, as you are not doing anything with it in main().
0
Ipang True I dont need the file pointer in main ll check out by creating another new file
0
Ipang I did and same weird characters appears .
0
Did you at least succeded in reading the line partially? mean does the content of the file get read into the string correctly, or the string contains totally "garbage"?
0
Ipang Yes it does but do I need to change the encoding or what?
0
Try to open the text file with regular text editor and see whether those weird characters also show up. If they do, then *maybe* the file was corrupted.
If not, then *maybe* you can try saving the file as plain text, and don't use Unicode encoding (UTF-xx)
Actually I'm not sure what to suggest cause Idk what's in that file ...
0
Ipang I did ,still outputs another weird characters in console,if the string contains garbage what should I do?
0
Okay, glad to hear that ... đ