Saving output

This is a discussion on Saving output within the C Programming forums, part of the General Programming Boards category; Compiling a 2d array that produces an output, how can I save that output to a file. Using "r" and ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22

    Saving output

    Compiling a 2d array that produces an output, how can I save that
    output to a file. Using "r" and "w" only transfers the the file, not . the output
    redirection operators < > also transfersn the file! Anyone have the dirction for this prob?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you could make a post that I could understand, I could possibly help you. However, since you didn't post much of anything intelligible, I can't help you with your assignment without totally guessing.

    Yes, you can open a file with "r" or "w", for reading and writing, respectively. The redirection operators < and > are for redirecting stdin and stdout respectively. What this has with your 2D array, I have no idea.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22
    It appears that you don't know either Mac!

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    OK, so help me out. What are you trying to do? Save a 2D array to a file?

  5. #5
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22
    No.... Save OUTPUT of the 2D array to a file!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But ... what do you mean when you say the "output of a 2D array"? I mean, a 2D array is just a bunch of numbers and certainly can't be compiled or give an output or anything. Do you mean you want them to appear nicely in a grid?

    If you could show us what you have written (involving this "r" and "w", perhaps) and what it's supposed to be doing that it isn't, then we can probably get you pointed in the right direction.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22
    As you can see the 2D array0 will produce an output of 3 lines with 6 numbers on each line. Being a simplified version of the mother array which will output tens of millions of numbers. i need to save those numbers to work on them. "R" amd "w"
    only transfers the code!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
       int main()
    {
    
        int list[3][6] = { 1,  2,  3,  4,  5,  6,
                           7,  8,  9, 10, 11, 12,
                          13, 14, 15, 16, 17, 18}; 
    
        int i, j;
        i = 0;
      
        for(j = 0; j < 6; j++) 
          printf( "&#37;4d", list[i][j]);
          printf( "\n"); 
         
        for( j = 0; j < 12; j++) {
           if((j>4)&&(j<11))
           continue;
         printf( "%4d", list[i][j]);} 
         printf( "\n");
        
        for(j=0; j<18; j++) {
             if((j>4) && (j<17))
         continue;
        printf( "%4d", list[i][j]); }
        printf( "\n");
        
       enum  {SUCCESS, FAIL};  
       void CharReadWrite(FILE *fin, FILE *fout);
    
    // defines two file pointers
        FILE *fptr1, *fptr2;
        char filename1[] = "LIST E.cpp"; //array 1 initiated with filename
        char filename2[] = "array1.cpp"; //writes to the above file
        int reval = SUCCESS;
    
        if((fptr1 = fopen(filename1, "w")) == NULL){                    
        printf( "Cannot open: %s\n", filename1);
        reval = FAIL; 
        }
         
         else if ((fptr2 = fopen(filename2, "r")) == NULL){
        printf( "Cannot open %s\n", filename2);
        reval = FAIL;
        } else {
          
        CharReadWrite(fptr2, fptr1);
        fclose(fptr2);
        fclose(fptr1);
        }
        system ("PAUSE");
        return reval;
        }
        void CharReadWrite(FILE *fin, FILE *fout)
        {
        int c;
        
        
        while((c=fgetc(fin)) != EOF) { 
        fputc( c, fout); 
        putchar(c);  
        }
        }
    Last edited by Rossco; 11-27-2007 at 07:27 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
        for( j = 0; j < 12; j++) {
           if((j>4)&&(j<11))
           continue;
         printf( "%4d", list[i][j]);} 
         printf( "\n");
        
        for(j=0; j<18; j++) {
             if((j>4) && (j<17))
         continue;
        printf( "%4d", list[i][j]); }
        printf( "\n");
    Just because you can do something, doesn't mean you should. I don't know what you're trying to do here, and I insist that you not tell me 'cause I want to keep it that way.

    All your output is happening in the CharReadWrite function, which does what it says: reads one file and writes another by characters. It knows nothing of list, so of course your 2D array is not going into the file. In fact, your comments (correctly) tell you that "array1.cpp" is written to the above file, which is "LIST E.cpp". [Side notes: // is not legal C; if you intend to write C, you should use /* comment */. And your filenames shouldn't end in .cpp then either. And all your variable declarations have to go to the top.]

    If you want what you see on the screen in your file, you can use fprintf -- it works the same way as printf, you just feed it a FILE * before the format string and the output goes to that FILE *, as
    Code:
    fprintf(fptr1, "%4d", list[i][j]);

  9. #9
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22
    Thanks tab.. I appreciate your help.. I'll try it out and let you know ...cheers

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Your previous topic:

    Redirection

    Whatever you're doing with trying to open and mess with .cpp files is totally screwed up and wrong.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Location
    AUSTRALIA
    Posts
    22
    I followed your directives Tab and it worked, I figured the fprintf() Fn was the key. Another problem arose tho' When I sent the mother array to a .txt file it produced a file of 117MB and when I opened it with notepad only a small portion of the file loaded. I thought files were bottomless (at least until the hard disk filled). Thanks again.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    [Side notes: // is not legal C; if you intend to write C, you should use /* comment */. And your filenames shouldn't end in .cpp then either. And all your variable declarations have to go to the top.]
    Actually, // comments and variable declarations in the middle of blocks are valid in C99. (// comments are also a common extension to C89.) If you want to write valid C89, you should avoid these constructs. Most of us write C89, so it's a good idea to do this.

    Quote Originally Posted by Rossco View Post
    I followed your directives Tab and it worked, I figured the fprintf() Fn was the key. Another problem arose tho' When I sent the mother array to a .txt file it produced a file of 117MB and when I opened it with notepad only a small portion of the file loaded. I thought files were bottomless (at least until the hard disk filled). Thanks again.
    Notepad can only open small files. I think its limit is something like 8MB. On older Windows systems, when you tried to open a larger file with notepad, it would give you an error and suggest trying wordpad. It's possible that in newer versions of Windows it doesn't give you an error, tries to load it anyway, but fails.

    Your only use of this
    Code:
    enum  {SUCCESS, FAIL};
    appears to be to return a success/fail value from main(). In that case, use the standard EXIT_SUCCESS and EXIT_FAILURE from <stdlib.h> instead of defining your own.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    [QUOTE=dwks;692614Notepad can only open small files. I think its limit is something like 8MB. On older Windows systems, when you tried to open a larger file with notepad, it would give you an error and suggest trying wordpad. It's possible that in newer versions of Windows it doesn't give you an error, tries to load it anyway, but fails.
    [/QUOTE]

    I took a 37 MB symbol file for my entire project, copied it into a new file 4 times to make a 155MB file, it loaded fine [it takes about forever to load or save the file]. I also counted the number of times I could find a particular symbol, and as far as I can tell, it's there 4 times. [And Notepad is using 450+ MB of memory at this point!]

    I much prefer to use Emacs or some such for larger files - If you try to insert into notepad within a large file, it also takes some time.

    This is the XP version of Notepad, but I'm pretty sure the same worked on Win2K - older versions certainly did have some limitations - I'm pretty sure the Win9X had a limit of 64KB size files.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Does anybody actually ever need to open that big of a text file in real life?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    Does anybody actually ever need to open that big of a text file in real life?
    I've had some really big log-files on the odd occasion, that I needed to search in - but most often you can use "grep" or similar, rather than open the file - but sometimes that's not feasible, because you want to see LOTS of what happened before a certain failure or some such... I've been known to load HUGE files into Emacs more than once...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 02:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21