# Thread: I can not write to a txt file using this program

1. ## I can not write to a txt file using this program

I run this program like this from the command line:

Code:
./writer mytext.txt
and then the file "mytext.txt" is created and cli notifies and prompts for writing into it, then I write something like "Hello World!" and finish it by giving the specific EOF of my OS and the program ends without any errors. But when I open the text file nothing is in there. I've also "cleaned up after myself" by flushing wherever I felt it's needed. Please help.

Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv){
FILE *fp;
int ch;
if(argc != 2){
printf("Wrong number of arguments entered. Filename: %s",argv[1]);
exit(EXIT_FAILURE);
}
fp = fopen(argv[1],"a+");
printf("File opened successfully. Write to file now:\n");
fflush(NULL);
while((ch = getchar()) != EOF){
fprintf(fp,"%c",ch);
}
fclose(fp);
return 0;
}

2. I compiled and ran your program and it worked for me to write "Hello world!" to the file.

3. Originally Posted by laserlight
I compiled and ran your program and it worked for me to write "Hello world!" to the file.
Ahhh... Thanks the problem then must be with this dumb mingw and windows. Honestly, it doesn't work for me. It seems that I should forget about windows if I want to learn C with less silly problems like this and switch to Linux.
Thanks for the help.

4. Well, before you give up, change the check for EOF to a check for '\n', then enter "Hello world!" instead of dealing with the issue of triggering end of file from interactive input. See if that works for you.

5. Switching to Linux is a very big step. Why don't you try clang or even Pelles IDE first ?

6. It seems that I should forget about windows if I want to learn C with less silly problems like this and switch to Linux.
Correct. For starters, you can also consider running your Linux distro on a Virtual Machine within Windows, or maybe even through WSL (not sure how limited it is).

7. How exactly did you signal EOF to the Windows CLI?

It should be CTRL-Z immediately following pressing enter.

Does this work?
./writer mytext.txt < testfile.txt

It will at least show whether it's an issue of getting EOF from the console, as opposed to any specific problem with your program.

8. I'm a little bit skeptical about Ctrl-Z, given that you say you are typing "./" in front of your program name, which a Windows shell won't recognize. Are you popping open a terminal other than the Windows command line? If so, Ctrl-Z becomes suspend (IIRC) and Ctrl-D is EOF. Also:
(1) I like that you're checking the command-line arguments; I don't like the actual error printing that you're doing. If they don't supply a filename, then argv[1] is going to be not worth printing; if there are too many arguments it will just show them one of them. I would prefer the more customary thing of showing a proper invocation in the usage statement.
(2) If you're going to print "File successfully opened" you should actually check the result of fopen to make sure that is true.

9. (Guys sorry I reply late.)
change the check for EOF to a check for '\n'
laserlight thanks I did this and this is strange it works! Because I expected the "Enter" on Windows to be equivalent of the "\r\n". (also, what if I want to type multiple lines?)
OldGuy2 I don't want to use an IDE. I'll take a look into them though. Thanks.
-Adrian Thanks, I also gave "WSL" a try and at first it was interesting but then annoyances showed up like problems in installing very well-known packages like gcc.(gcc comes NOT pre-installed with it!)
Salem & tabstop Sorry I didn't mention that I'm using git bash since 1. the windows cmd was difficult to open (no "open Git Bash here" option on the right click) and 2. cmd disappears ASA the program is run unless you add some unnecessary codes to EVERY single program.
I used CTRL+C to end the program. I was under the impression that anything that ends the application can be considered as EOF. I know it's silly CTRL+Z is giving the error: 2 [sig] bash 8328! sigpacket:rocess: Suppressing signal 18 to win32 process (pid 22064) and I have no idea why bash suppresses CTRL+Z. Also CTRL+D is doing nothing. (Also about the error messages you're right, I was just testing, thanks).

P.S.: Guys I've just found "Babun" and I'm going to give it a try now to see how useful it can be. (installing a GNU/Linux distro on an external hard drive is also an option anyways).

10. In terms of cmd disappearing: why not just open a command prompt (hit the Win key, type "cmd", and off you go) and run the command there? The program will run in that window and you'll see the output etc. I guess that depends a bit on whether you're compiling locally. I don't remember having too much trouble getting MinGW installed, but (for example) at work I have trouble keeping that in my path so I often have to start when I open a cmd window by doing set PATH=%PATH%;h:\codeblocks\mingw\bin (you can tell how I installed MinGW there, but if you don't like a GUI you can definitely install it by itself).

Yeah, Ctrl-C is just going to immediately abort; if you were flushing after every character that would (a) be very inefficient but (b) would make sure that the file was written even if you abort instead of end normally. When you say Ctrl-D is doing nothing, it's not ending your program at all? (I would expect any version of bash to send EOF on a Ctrl-D, but maybe the Windows version is weird.)

11. Originally Posted by tabstop
In terms of cmd disappearing: why not just open a command prompt (hit the Win key, type "cmd", and off you go) and run the command there?
Well, I'm a bit confused now, well for every practicing code I might create a different directory so...?? cmd always gets initialized in my user directory. but for example, the directory of my code is in D:\\foo\bar. Maybe I don't understand you correctly here.

Originally Posted by tabstop
When you say Ctrl-D is doing nothing, it's not ending your program at all? (I would expect any version of bash to send EOF on a Ctrl-D, but maybe the Windows version is weird.)
Yes, it's literally doing NOTHING. I think it's a git bash thing because I've had other problems with it too before. doing other stuff with it. Hopefully, "Babun" will be better. I'll post here when I tried it.

12. Originally Posted by narniat
Well, I'm a bit confused now, well for every practicing code I might create a different directory so...?? cmd always gets initialized in my user directory. but for example, the directory of my code is in D:\\foo\bar. Maybe I don't understand you correctly here.
There are all sorts of things you can do in a command shell, and one of them is change directories! It's even cd, just like in a *nix shell. Type cd d:\foo\bar and you're there.