+ Post New Thread
Results 1 to 10 of 10
  1. #1
    Junior Member level 2
    Points: 214, Level: 2

    Join Date
    Feb 2018
    Posts
    20
    Helped
    0 / 0
    Points
    214
    Level
    2

    writing to a text file

    Dear all,

    How can I write the displayed output in the command prompt to a text file?

    Code:
    #include<stdio.h>
        #include<string.h>
        int main()
        {
            /*declare and initialise variable*/
            char message[32][114],buffer[114];
            int i=1;
            FILE *file_in;
            FILE *file_out;
    
            file_in=fopen("exon11.txt", "r");
            file_out=fopen("1.txt", "w");
    
            /*stores and prints the data from the string*/
            while(fgets(buffer,114,file_in))
                {
                    strcpy(message[i],buffer);
                    fputs(message[i],file_out);
                    printf(">%d\n%s\n",i,message[i]);
                    i++;
            }
    
            getchar();
            return 0;
    
        }
    tq very much.

  2. #2
    Advanced Member level 5
    Points: 13,962, Level: 28
    pancho_hideboo's Avatar
    Join Date
    Oct 2006
    Location
    Real Homeless
    Posts
    2,090
    Helped
    579 / 579
    Points
    13,962
    Level
    28

    Re: writing to a text file

    % cat > hoge.txt

    Ctrl-c



    •   AltAdvertisment

        
       

  3. #3
    Super Moderator
    Points: 73,223, Level: 66
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,992
    Helped
    3968 / 3968
    Points
    73,223
    Level
    66

    Re: writing to a text file

    How can I write the displayed output in the command prompt to a text file?
    The command prompt comes from the OS that executes your program, not the program itself. If you want to save the result of the printf() command to a file (capture the text output) just add "> MyFile" to the end of the line that starts your program. "MyFile" can be any valid file name, including a path if needed.

    For example, if "MyProgram" produced the text "Hello World", typing this:
    MyProgram > result.txt
    would produce a file called "result.txt" with "Hello World" in it.
    If you want to add to an existing file rather than create a new one, use >> instead of >. If the file doesn't exist, it will create it then add to it.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  4. #4
    Junior Member level 2
    Points: 214, Level: 2

    Join Date
    Feb 2018
    Posts
    20
    Helped
    0 / 0
    Points
    214
    Level
    2

    Re: writing to a text file

    I am sorry. I do not understand. Can you give some example where can I put the suggested command?



    •   AltAdvertisment

        
       

  5. #5
    Advanced Member level 5
    Points: 13,962, Level: 28
    pancho_hideboo's Avatar
    Join Date
    Oct 2006
    Location
    Real Homeless
    Posts
    2,090
    Helped
    579 / 579
    Points
    13,962
    Level
    28



  6. #6
    Junior Member level 2
    Points: 214, Level: 2

    Join Date
    Feb 2018
    Posts
    20
    Helped
    0 / 0
    Points
    214
    Level
    2

    Re: writing to a text file

    Can this command is used in code block? Im using window 10
    Last edited by dayana42200; 8th September 2018 at 13:37. Reason: adding info



    •   AltAdvertisment

        
       

  7. #7
    Advanced Member level 5
    Points: 13,962, Level: 28
    pancho_hideboo's Avatar
    Join Date
    Oct 2006
    Location
    Real Homeless
    Posts
    2,090
    Helped
    579 / 579
    Points
    13,962
    Level
    28

    Re: writing to a text file

    Quote Originally Posted by dayana42200 View Post
    Can this command is used in code block? Im using window 10
    https://superuser.com/questions/74127/tee-for-windows



  8. #8
    Super Moderator
    Points: 73,223, Level: 66
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    11,992
    Helped
    3968 / 3968
    Points
    73,223
    Level
    66

    Re: writing to a text file

    dayana42200, are you trying to write the text file from inside the program or are you trying to capture the output to screen into a text file?

    Put another way, when the printf() in your program sends text to the screen, are you trying to save it to a file as well, from inside the program or are you trying to save the characters that you see displayed via the screen? The first method is a simple file open/write/close in your code, the second requires the operating system to redirect or duplicate (tee) screen output to a file so it is done at OS level from outside your program.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  9. #9
    Junior Member level 2
    Points: 214, Level: 2

    Join Date
    Feb 2018
    Posts
    20
    Helped
    0 / 0
    Points
    214
    Level
    2

    Re: writing to a text file

    ok its working. I at fclose() command. Thank you for all the helps. Can you guys explain to me, why the data is written in the text file when fclose() command is used?



  10. #10
    Advanced Member level 5
    Points: 13,962, Level: 28
    pancho_hideboo's Avatar
    Join Date
    Oct 2006
    Location
    Real Homeless
    Posts
    2,090
    Helped
    579 / 579
    Points
    13,962
    Level
    28

    Re: writing to a text file

    Quote Originally Posted by dayana42200 View Post
    why the data is written in the text file when fclose() command is used?
    http://man7.org/linux/man-pages/man3/fclose.3.html



--[[ ]]--