C File Input/Output on Windows - c

First of all, this is my fist stack overflow question, so forgive me if I format this wrong.
I am a beginner at C, and I am up to a point in my book on File i/o. The following code, which is supposed to print the lines to test.txt, doesn't create a new txt file or... do anything.
I am running Code Blocks 16.01 on Windows. Is this code designed for another OS?
#include <stdio.h>
#include <stdlib.h>
main() {
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
Ok, so removing the slash makes it work. In the original code, it is 'fopen("/tmp/test.txt", "W+");'
Shouldn't this create the file in folder tmp?

Try removing the front-slash from the file name. You seem to be doing everything properly, the slash might be the problem. If not, let us know.
Edit: When I wrote my comment your fopen used "/test.txt" and not "/temp/test.txt", do you have the "temp" folder created in the directory the application is running from? If not, try creating it. Or remove it altogether and try creating the text file within the directory the application is running from.

Use double // in windows for navigate through directory.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
/*
file path in windows should be like this= C:\\users\\r.maurya\\Desktop\\Downloads\\file.txt
*/
fp = fopen("C:\\users\\r.maurya\\Desktop\\Downloads\\file.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
return 0;//Optional, On success of program
}

Related

C fopen creating file in my user directory instead of desktop Mac

I am messing around with reading and writing to files in C. I coded a program, which is saved on my desktop. I then compiled it using the terminal, also on the desktop. But when I ran the program, it said that everything was working, but the file was nowhere to be found. I used the spotlight search, and it said that my file was located in my /Users/johndoe directory. Here is the code. Thanks in advance for helping.
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE* fp1 = fopen("boop.txt", "a");
fputs("Hello World\n", fp1);
fclose(fp1);
FILE* fp2 = fopen("boop.txt", "r");
char* output = (char *) malloc(20);
fgets(output, 20, fp2);
printf("%s", output);
fflush(stdout);
return 0;
}
What do you mean when you say the terminal is "also on the desktop"? I suspect you mean that the icon the execute the terminal is on the desktop, but the shell that is running inside the terminal has a working directory of $HOME. When you run the program from the shell, type pwd to see the directory it is running in. That is the directory in which the files will be created.

Can't find a source file at" /getc.c" eclipse c ubuntu

just installed eclipse on my linux and trying working with files.
I wanted to use fgetc function but it seems that its not working..
while debugging: even if Im using step over its crush, and while letting it run its just dont do anything.
its happen also for every function related to files like fscanf,fgets etc..
the error messege is:
Can't find a source file at "/build/glibc-OTsEL5/glibc-2.27/libio/getc.c"
Locate the file or edit the source lookup path to include its location.
any ideas?
thnk's in advanced
and this is my code:
#include <stdio.h>
#include <stdlib.h>
int main(){
func();
return 0;
}
void func(){
int ch;
int fd = open("out.txt", O_RDONLY);
if(fd < 0)
perror("fd");
ch = fgetc(fd);
printf("%d",ch);
}
The error message comes from the debugger. It indicates that whoever built glibc for your system did not add source files to the debugging information. As a result, stepping through system library functions such as fgetc is very confusing. But this is independent of your actual problem.
You cannot mix file descriptor functions like open with file stream functions like fgetc. The compiler will have print a type mismatch warning; you really should not ignore these.
Something like this should fix the type error:
File *fp = fopen("out.txt", "r");
if (fp == NULL) {
perror("fopen");
return 1;
}
ch = fgetc(fp);
If you want to keep using unbuffered I/O and open, you will have to use the read function instead of fgetc to read bytes.

Cannot access file from other drive

I am using windows 7 64bit OS and DOS box turbo C++. I want to write a simple program to read a text file containing a single integer from E drive of my machine. The file name is t.txt. I have written the following code:
#include <stdio.h>
#include <conio.h>
#include <dir.h>
#include <stdlib.h>
int main(void)
{
FILE *input;
int data;
if ( (input = fopen("E:\\t.txt","r")) == NULL)
printf("Error: Unable to open");
else
{
fscanf(input,"%d",&data);
printf("successfully read in %d",data);
}
fclose(input);
input=NULL;
getch();
}
But this program is unable to access the file and every time it gives an output like:
Error: Unable to open
What is the problem with this code?
Please help.
Yes!!! got it.. Thank you Michael. I tried to mount E: in dosbox and it has run fine.
Problem must be with the usage of file path
Solution given below worked with me in Ubuntu
just try it
char *file = "E:\\t.txt";
FILE *fp = fopen(file, "r");
And verify whether you are using the correct path
Have a good day

File I/O management in C

My first post :), am starting out with C language as basic learning step into programming arena. I am using following code which reads string from text file, makes directory with that string name and opens a file for writing in that created directory. But am not able to create a file inside directory made, here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
#include <string.h>
int main()
{
char file_name[25], cwd[100];
FILE *fp, *op;
fp = fopen("myfile.txt", "r");
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
fgets(file_name, 25, fp);
_mkdir(file_name);
if (_getcwd(cwd,sizeof(cwd)) != 0)
{
fprintf(stdout, "Your dir name: %s\\%s\n", cwd,file_name);
op = fopen("cwd\\file_name\\mynewfile.txt","w");
fclose(op);
}
fclose(fp);
return 0;
}
What you need is to store the file name (with the path) in a c-string before opening. What you are opening is cwd\file_name\mynewfile.txt. I doubt that your directory is named cwd.
A sample could could be:
char file_path[150];
sprintf(file_path, "%s\\%s\\mynewfile.txt", cwd, file_name);
op = fopen(file_path,"w");
use
#include <sys/stat.h>
#include <sys/types.h>
instead of
#include <direct.h>
and modify
op = fopen("cwd\\file_name\\mynewfile.txt","w”);
I see you are using the return values. That is a good start for a beginner. You can refine your error messages by including "errno.h". Instead of printing your own error messages call
printf("%s", strerror(errno));
You get more precise error messages that way.
op = fopen("cwd\\file_name\\mynewfile.txt","w”);
You’re actually passing the string literals “cwd” and “file_name” as part of the path of the file, when I think you actually mean to put the contents of the variables with those names in there. You will probably have to piece together a string for the path. Try looking into strcat()
http://www.cplusplus.com/reference/cstring/strcat/

SImple C Program opening a file

I'm trying to make a program to open a file, called "write.txt".
#include <stdio.h>
main() {
FILE *fp;
fp = fopen("write.txt", "w");
return 0;
}
Should this work? Because it returns nothing.
Other than an old variant of main, there's not really much wrong with that code. It should, barring errors, create the file.
However, since you're not checking the return value from fopen, you may get an error of some sort and not know about it.
I'd start with:
#include <stdio.h>
#include <errno.h>
int main (void) {
FILE *fp;
fp = fopen ("write.txt","w");
if (fp == NULL) {
printf ("File not created okay, errno = %d\n", errno);
return 1;
}
//fprintf (fp, "Hello, there.\n"); // if you want something in the file.
fclose (fp);
printf ("File created okay\n");
return 0;
}
If you're adamant that the file isn't being created but the above code says it is, then you may be a victim of the dreaded "IDE is working in a different directory from what you think" syndrome :-)
Some IDEs (such as Visual Studio) will actually run your code while they're in a directory like <solution-name>\bin or <solution-name>\debug. You can find out by putting:
system ("cd"); // for Windows
system ("pwd") // for UNIXy systems
in to your code to see where it's running. That's where a file will be created if you specify a relative path line "write.txt". Otherwise, you can specify an absolute path to ensure it tries to create it at a specific point in the file system.
What did you expect it to 'return' - it opens a file, on most platforms creating one if it doesn't exist.
You should probably fclose(fp) the file at the end.
I think you want to print the contents of file write.txt. (Assume it contains characters).
#include <stdio.h>
int main()
{
FILE *fp,char ch;
fp=fopen("write.txt","r");
if(fp==NULL)
{
printf("Some problem in opening the file");
exit(0);
}
else
{
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
}
fclose(fp);
return 0;
}
I think you should study some more fundamentals in C before you start attempting to work with files. A return means some data is passed back to the calling code from the called function.In this case you return 0 at the end of your program. You did not do anything with your FILE pointer except cause a new file to be created...

Resources