| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
a simple problem that takes me 2 hours
Dear all,
I want to read some information from hundreds of files and write them into the one file. But once I reopen the file to write, the pointer is always in the very beginning of the file. So I want to make the pointer stay in the end of the file when I reopen the file to write. I take 2 hours to deal with it. I used the function "fseek" but it did not work. Anyone can give me a little help that can help me a lot? The following is my codes. Thank you for help me go through it. #include <stdio.h>; FILE *stream; FILE *stream2; void main( void ) { char s[8100]; int i; stream = fopen( "1cdlg.concise", "r+" ); if( stream == NULL ) printf( "The file fscanf.out was not opened\n" ); else { fscanf( stream, "%s", s); stream2 = fopen( "new.txt", "w" ); /*fseek( stream2, 0L,SEEK_END);*/ fprintf( stream2, "\n%s", s); for (i=0; i<10000; i++) { fscanf( stream, "%s", s); if (s[0]=='D' && s[1]=='S' && s[2]=='S' ) { fprintf( stream2, "\n%s", s); break; } } fscanf( stream, "%s", s); fprintf( stream2, "\n%s\n", s); fscanf( stream, "%s", s); fprintf( stream2, "\n%s\n", s); fscanf( stream, "%s", s); fprintf( stream2, "\n%s\n", s); fclose( stream2 ); fclose( stream ); } } |
|
#2
|
||||
|
||||
|
You'll probably get a better response asking this in the C forum, being a C question. Mods can we move this for him
.Personally I'm not sure this is really practical. If you close the file from read and open in write, why not just seek again? Correct me if I'm wrong but doesn't r+ also allow writing, if so then you probably don't need another pointer. I'm pretty new to C sorry. You might also want to use Code tags when you post code to preserve the proper indentation. Take care, Mark. |
|
#3
|
||||
|
||||
|
Disclaimer, my C skills are rusty, so I didn't really look at your code. Instead I read your description above.
Some pseudo code for how I would write the program: Code:
OPEN write file
LOOP through other files
OPEN read file
READ from read file
WRITE to write file
CLOSE read file
END LOOP of other files
CLOSE write file
From your description (not the code), it sounds like you are opening and closing the write file every time. |
|
#4
|
||||
|
||||
|
moved
|
|
#5
|
||||
|
||||
|
Code:
stream = fopen( "1cdlg.concise", "r+" ); instead of "r+", use one of the mode 'a'. It opens the file with writing (but not reading) permissions and automatically moves the file pointer to the end. For reading permissions, use "a+".
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > a simple problem that takes me 2 hours |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|