| | |
Reading text file and copying to new one. Bit file editing too.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 32
Reputation:
Solved Threads: 4
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
But I would like to read the source line by line, with a string long enough for the reading. Something like:
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:
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.
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
C++ Syntax (Toggle Plain Text)
#include <stdio.h> void main() { char car; FILE *pD, pS; //Destination and Source pD = fopen("DEST.TXT","wt"); PS = fopen("SOURCE.TXT","rt"); do { car = fgetc(pS); fprintf(pD,"%c", car); } while(c!=EOF); fcloseall(); }
C++ Syntax (Toggle Plain Text)
char instring[101], fin; pD = fopen("DEST.TXT","wt"); pS = fopen("SOURCE.TXT","rt"); do { fgets(instring,100,pS); fprintf(pD,"%s", instring); } while( ????? );
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();
}Thanks in advance.
Last edited by emotionalone; Oct 12th, 2008 at 1:51 pm.
•
•
Join Date: Oct 2008
Posts: 122
Reputation:
Solved Threads: 6
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.
C++ Syntax (Toggle Plain Text)
ifstream PS("SOURCE.TXT"); ofstream pD; pD.open("DEST.TXT"); string line; while( getline(PS, line) ) { pD << line << '\n'; } pD.close();
Last edited by Liszt; Oct 12th, 2008 at 7:33 pm.
or this using FILE*
C++ Syntax (Toggle Plain Text)
char iobuf[255] = {0}; while( fgets(iobuf, sizeof(iobuf), pS) ) { fprintf(iobuf); }
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.
![]() |
Similar Threads
- comparing file in c (C)
- Problem with strtok() for data structure creation (C++)
- reading records from a .dbf file (Visual Basic 4 / 5 / 6)
- Moving database to new server (MySQL)
- C++ Reading from a text file (C++)
- error C2375: 'my_strdup' : redefinition; different linkage (C++)
- I/O question. using vc++ read char by char (C++)
- Reading data into files (C++)
- problem with convert jumbled text file to unjumbled text file (C)
Other Threads in the C++ Forum
- Previous Thread: LNK2001 and LNK2019 problem!
- Next Thread: Function return question?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






