C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 8th, 2005, 08:36 PM
aioshuang aioshuang is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 3 aioshuang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 46 sec
Reputation Power: 0
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 );
}
}

Reply With Quote
  #2  
Old July 10th, 2005, 09:18 PM
netytan's Avatar
netytan netytan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Knighton, UK
Posts: 29 netytan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 7 m 44 sec
Reputation Power: 0
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #3  
Old July 11th, 2005, 11:33 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
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.

Reply With Quote
  #4  
Old July 11th, 2005, 06:37 PM
Viper_SB's Avatar
Viper_SB Viper_SB is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Canada
Posts: 330 Viper_SB User rank is Private First Class (20 - 50 Reputation Level)Viper_SB User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 5 h 3 sec
Reputation Power: 16
moved

Reply With Quote
  #5  
Old July 12th, 2005, 02:22 AM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 24 m 9 sec
Reputation Power: 14
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.



Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > a simple problem that takes me 2 hours


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap