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

#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();
}

But I would like to read the source line by line, with a string long enough for the reading. Something like:

char instring[101], fin;
    pD = fopen("DEST.TXT","wt");
    pS = fopen("SOURCE.TXT","rt");
    do
    {
      fgets(instring,100,pS);
      fprintf(pD,"%s", instring);
    }
    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.

Dani AI

Generated

Quick summary and what to change first: since your class restricts you to stdio.h, prefer a fgets/fputs loop that checks the return value instead of testing feof. ’s getline approach is fine for C++, but in C use fgets and fputs (or fprintf(pD, "%s", buf)) and avoid reading with a char variable for fgetcfgetc returns an int and must be compared to EOF. was on the right track with fgets, but the write call needs the file pointer (fputs(buf, pD) or fprintf(pD, "%s", buf)), not just the buffer.

A robust stdio-only line-copy pattern (handles lines longer than the buffer):

#include <stdio.h>
#include <string.h>

int main(void) {
    FILE *src = fopen("SOURCE.TXT","r");
    FILE *dst = fopen("DEST.TXT","w");
    char buf[128];

    if (!src || !dst) { perror("fopen"); return 1; }

    while (fgets(buf, sizeof buf, src) != NULL) {
        if (fputs(buf, dst) == EOF) { perror("write"); break; }
        /* if the buffer did not end with '\\n' the line was truncated; copy remainder */
        if (buf[0] && buf[strlen(buf)-1] != '\n') {
            int ch;
            while ((ch = fgetc(src)) != EOF) {
                if (fputc(ch, dst) == EOF) { perror("write"); break; }
                if (ch == '\n') break;
            }
        }
    }

    fclose(src);
    fclose(dst);
    return 0;
}

About the record-swapping routine: the surprising seeks/writes you see are almost certainly caused by a few bugs in the posted code — missing semicolon after the typedef, typos in the type name, calling fwrite with Reg (not &Reg), and doing sizeof(TGuest) * -1 (unsigned arithmetic can wrap). Fix those, check return values, and use a negative offset cast to long when seeking backwards. Example corrected core loop:

typedef struct { char Name[21]; char LastName[21]; char GuestCode[7]; char Gender; int Table; } TGuest;

/* ...open file as "r+b"... */

TGuest reg;
while (fread(&reg, sizeof reg, 1, f) == 1) {
    if (reg.Table == 5 || reg.Table == 6) {
        reg.Table = (reg.Table == 5) ? 6 : 5;
        if (fseek(f, - (long)sizeof reg, SEEK_CUR) != 0) { perror("fseek"); break; }
        if (fwrite(&reg, sizeof reg, 1, f) != 1) { perror("fwrite"); break; }
        fflush(f);
    }
}

Final notes: use int main(void) and close files individually (not fcloseall), add perror checks to spot failures, and use ftell prints while debugging. Also be aware of struct padding/endianness if the file must be portable — text formats are simpler across tools and compilers.

Recommended Answers

All 2 Replies

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.

ifstream PS("SOURCE.TXT");
ofstream pD;
pD.open("DEST.TXT");
string line;

while( getline(PS, line) )
{
	pD << line << '\n';
}
pD.close();

or this using FILE*

char iobuf[255] = {0};
while( fgets(iobuf, sizeof(iobuf), pS) )
{
     fprintf(iobuf);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.