- 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; }

29th Nov 2021, 12:05 PM
Coderalgeria
Coderalgeria - avatar
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.
29th Nov 2021, 1:56 PM
Coderalgeria
Coderalgeria - avatar
+ 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); }
29th Nov 2021, 5:00 PM
Abdelkader Chhaibi
Abdelkader Chhaibi - avatar
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().
29th Nov 2021, 1:17 PM
Ipang
0
Ipang True I dont need the file pointer in main ll check out by creating another new file
29th Nov 2021, 1:20 PM
Coderalgeria
Coderalgeria - avatar
0
Ipang I did and same weird characters appears .
29th Nov 2021, 1:25 PM
Coderalgeria
Coderalgeria - avatar
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"?
29th Nov 2021, 1:28 PM
Ipang
0
Ipang Yes it does but do I need to change the encoding or what?
29th Nov 2021, 1:33 PM
Coderalgeria
Coderalgeria - avatar
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 ...
29th Nov 2021, 1:39 PM
Ipang
0
Ipang I did ,still outputs another weird characters in console,if the string contains garbage what should I do?
29th Nov 2021, 1:52 PM
Coderalgeria
Coderalgeria - avatar
0
Okay, glad to hear that ... 👍
29th Nov 2021, 1:57 PM
Ipang