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

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    42

    Unhappy 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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I compiled and ran your program and it worked for me to write "Hello world!" to the file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Quote Originally Posted by laserlight View Post
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Switching to Linux is a very big step. Why don't you try clang or even Pelles IDE first ?

  6. #6
    Guest
    Guest
    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. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #9
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    (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).
    Last edited by narniat; 10-21-2018 at 12:21 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #11
    Registered User
    Join Date
    Oct 2018
    Posts
    42
    Quote Originally Posted by tabstop View Post
    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.

    Quote Originally Posted by tabstop View Post
    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. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by narniat View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program to get RAM and write to text file
    By stev100 in forum C Programming
    Replies: 4
    Last Post: 05-14-2015, 02:01 PM
  2. Replies: 3
    Last Post: 04-13-2015, 10:38 PM
  3. Replies: 2
    Last Post: 08-30-2014, 11:07 PM
  4. Basic file read, file write program
    By starwarsyeah in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2011, 03:23 PM
  5. Replies: 2
    Last Post: 03-04-2010, 04:19 AM

Tags for this Thread