Reading text file and copying to new one. Bit file editing too.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 32
Reputation: emotionalone is an unknown quantity at this point 
Solved Threads: 4
emotionalone emotionalone is offline Offline
Light Poster

Reading text file and copying to new one. Bit file editing too.

 
0
  #1
Oct 12th, 2008
Hello,

I'm trying to simply copy the text from one .txt and create a new one and copying it to a new txt. I'm actually adding more text to that new file but that's not the part I'm having problems with.

I could go through the entire .txt with this
  1. #include <stdio.h>
  2. void main()
  3. {
  4. char car;
  5. FILE *pD, pS; //Destination and Source
  6. pD = fopen("DEST.TXT","wt");
  7. PS = fopen("SOURCE.TXT","rt");
  8. do
  9. {
  10. car = fgetc(pS);
  11. fprintf(pD,"%c", car);
  12. }
  13. while(c!=EOF);
  14. fcloseall();
  15. }
But I would like to read the source line by line, with a string long enough for the reading. Something like:
  1. char instring[101], fin;
  2. pD = fopen("DEST.TXT","wt");
  3. pS = fopen("SOURCE.TXT","rt");
  4. do
  5. {
  6. fgets(instring,100,pS);
  7. fprintf(pD,"%s", instring);
  8. }
  9. while( ????? );
I just don't know what to put in the while or how else to read the file line by line until the end of the file. Knowing how many lines the source has might help, but I'm sure there's gotta be a beter way.
In our class we're not using iostream.h, just stdio.h for I/O.

Also, What's wrong with this piece of code:
#include <stdio.h>
typedef struct Guest
{
  char Name[21];
  char LastName[21];
  char GuestCode[7];
  char Gender;
  int Table;
} TGuest

void Table_Swap()
{
  FILE *pF;
  TGues Reg;
  pF = fopen("GUESTS.DAT","r+b");
  if( pF==NULL )
  {
    printf("FILE ERROR");
    getch();
    return;
  }
  while( (fread(&Reg,sizeof(TGuest),1,pF)) > 0 )
  {
    if( Reg.Table == 5 )
    {
      Reg.Table = 6;
      fseek(pF,sizeof(TGuest)*-1,SEEK_CUR);
      fwrite(Reg,sizeof(TGuest),1,pF);
      //Doesn't need to be  fwrite(&Reg,sizeof(TInvitado),1,pF);
      //right? I tried both ways, shouldn't be that.
    }
    if( Reg.Table == 6 )
    {
      Reg.Table = 5;
      fseek(pF,sizeof(TGuest)*-1,SEEK_CUR);
      fwrite(Reg,sizeof(TGuest),1,pF);
    }
  }
  fclose(pF);
}

void main()
{
  Table_Swap();
}
What that code's supposed to do is change the people from table 5 to table 6 and viceversa. While doing the tracing I noticed that after it would find a guest with table 5 or 6, and it would do the fseek and or fwrite (can't tell when), it seems to go back up to the 2nd entry of the .DAT file. Any thoughts?

Thanks in advance.
Last edited by emotionalone; Oct 12th, 2008 at 1:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 122
Reputation: Liszt is an unknown quantity at this point 
Solved Threads: 6
Liszt Liszt is offline Offline
Junior Poster

Re: Reading text file and copying to new one. Bit file editing too.

 
0
  #2
Oct 12th, 2008
Wonder if it could be possible for you to use ifstream and getline to get the lines and then use this line to put to you output file. This should read the file to the end.

  1. ifstream PS("SOURCE.TXT");
  2. ofstream pD;
  3. pD.open("DEST.TXT");
  4. string line;
  5.  
  6. while( getline(PS, line) )
  7. {
  8. pD << line << '\n';
  9. }
  10. pD.close();
Last edited by Liszt; Oct 12th, 2008 at 7:33 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Reading text file and copying to new one. Bit file editing too.

 
0
  #3
Oct 12th, 2008
or this using FILE*
  1. char iobuf[255] = {0};
  2. while( fgets(iobuf, sizeof(iobuf), pS) )
  3. {
  4. fprintf(iobuf);
  5. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC