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?
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?
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.
It appears that you don't know either Mac!
OK, so help me out. What are you trying to do? Save a 2D array to a file?
No.... Save OUTPUT of the 2D array to a file!
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.
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( "%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 08:27 PM.
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.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");
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 *, asCode:fprintf(fptr1, "%4d", list[i][j]);
Thanks tab.. I appreciate your help.. I'll try it out and let you know ...cheers
Your previous topic:
http://cboard.cprogramming.com/showthread.php?t=96121
Whatever you're doing with trying to open and mess with .cpp files is totally screwed up and wrong.
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.
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.[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.]
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
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.Code:enum {SUCCESS, FAIL};
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.
[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.
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.