0

Hey guys Idk why when i creat a new file and i write on it when i try to read from by program the pointer always stay in null va

#include<stdio.h> #include<stdlib.h> void prenom(){ char c[20]; FILE *f=fopen("ex.txt","r"); if (f==NULL) perror("error"); fgets(c,20,f); printf("%s",c); fclose(f); } int main(){ prenom(); }

10th Jan 2022, 11:38 AM
Coderalgeria
Coderalgeria - avatar
7 Réponses
+ 2
Isho thank you very much it worked can u please give me a lesson or video to clearify that thing more and ll apperciated it
10th Jan 2022, 9:30 PM
Coderalgeria
Coderalgeria - avatar
+ 1
The code (copy-pasted) is working for me. Make sure the file 'ex.txt' exists and is in the same directory as your exe file (assuming you're on Windows).
10th Jan 2022, 2:22 PM
Isho
Isho - avatar
+ 1
The file must be next to the exe for this to work. File paths are relative to the program's working directory which, when double-clicking the exe, is the same as the exe directory. So, yeah, put the text file next to the exe and it should work.
10th Jan 2022, 9:05 PM
Isho
Isho - avatar
+ 1
Here are a couple of articles on Wikipedia that briefly touch the subject: Working directory: https://en.wikipedia.org/wiki/Working_directory Paths: https://en.wikipedia.org/wiki/Path_(computing) also contains a brief comparison between relative vs absolute paths. Though a good way to get comfortable with file paths is to use a terminal/console/cmd/call it whatever for a while. Learning file manipulation through the command line (powershell, for example) will help a lot, since you'll have to actually type the paths in (correctly!) instead of relying on the GUI to figure that out.
10th Jan 2022, 10:10 PM
Isho
Isho - avatar
0
Isho Acctually the file is in desktop and exe one in documents does it influence on my program?
10th Jan 2022, 8:37 PM
Coderalgeria
Coderalgeria - avatar
0
Which thing? working directory? relative paths?
10th Jan 2022, 9:48 PM
Isho
Isho - avatar
0
@Isho both if u can
10th Jan 2022, 9:49 PM
Coderalgeria
Coderalgeria - avatar