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(); }
7 Antworten
+ 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
+ 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).
+ 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.
+ 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.
0
Isho Acctually the file is in desktop and exe one in documents does it influence on my program?
0
Which thing? working directory? relative paths?
0
@Isho both if u can