1

I'm writing a basic program that copies a string from an existing text file and copies the text into a new text file. I'm almost there but I'm having a few small issues. First, I output the line of text to the screen after copying and it's giving me 3 random characters after the string. I want to know why this is happening. Also, the program is creating the new text file but not putting the string into the file.

Here's my code:

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

int main(void)
{
char content[80];
char newcontent[80];

//Step 1: Open text files and check that they open//
FILE *fp1, *fp2;
fp1 = fopen("details.txt","r");
fp2 = fopen("copydetails.txt","w");

    if(fp1 == NULL || fp2 == NULL)
    {
    printf("Error reading file\n");
    exit(0);
    }
    printf("Files open correctly\n");
//Step 2: Get text from original file//
while(fgets(content, strlen(content), fp1) !=NULL)
    {
    fputs (content, stdout);
    strcpy (content, newcontent);
    }
    printf("%s", newcontent);
printf("Text retrieved from original file\n");

//Step 3: Copy text to new file//
    while(fgets(content, strlen(content), fp1) !=NULL)
        {
            fprintf(fp2, newcontent);
        }
        printf("file created and text copied to it");
//Step 4: Close both files and end program//
        fclose(fp1);
        fclose(fp2);
return 0;
}
adohertyd
  • 2,689
  • 19
  • 58
  • 78

4 Answers4

1

This modified version of the program would do the job:

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

int main(void)
{
    char content[80];

    //Step 1: Open text files and check that they open//
    FILE *fp1, *fp2;
    fp1 = fopen("details.txt","r");
    fp2 = fopen("copydetails.txt","w");

    if(fp1 == NULL || fp2 == NULL)
    {
        printf("\nError reading file\n");
        exit(0);
    }
    printf("\nFiles open correctly\n");

    //Step 2: Get text from original file//
    while(fgets(content, sizeof(content), fp1) !=NULL)
    {
        fprintf(fp2, "%s", content);
    }

    printf("File created and text copied to it\n\n");

    //Step 4: Close both files and end program//
    fclose(fp1);
    fclose(fp2);
    return 0;
}
1

In strcpy the order of sec and dest is reversed

Also ideally I would not copy but rather concat to the buffer.

Sid Malani
  • 2,078
  • 1
  • 13
  • 13
  • I've done the reverse and that has eliminated the strange characters however it's now outputting the content of the original text file twice. – adohertyd Nov 30 '11 at 22:40
  • You should ideally open both files read from one in buffer. Write buffer to second file until all is read. Then close both files. – Sid Malani Nov 30 '11 at 22:46
  • What would be the code for that? I'm not very familiar with using buffers. Our lecturer has only briefly touched on that. – adohertyd Nov 30 '11 at 22:49
  • Remove second loop, change fputs to fputs(fp2, content); in first loop – Sid Malani Nov 30 '11 at 23:00
1

You need to change:

while(fgets(content, strlen(content), fp1) !=NULL)

you need the sizeof the array content, not the length.

while(fgets(content, sizeof(content), fp1) !=NULL)

Even if you had initialised content before using it strlen() would return 0 and you would have read nothing from the file.

Also, if you want to re-read the input file when writing the new file you need to either fclose() the input file and fopen() it or rewind() it.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • I've altered the strlen to sizeof but it's still not writing to the new file. Any other ideas? – adohertyd Nov 30 '11 at 22:41
  • Did you alter both of the `fgets()` to use `sizeof` and ensure you are at the beginning of the the input file again? – hmjd Nov 30 '11 at 22:43
  • You my friend are a life saver. Would you mind explaining why I had to close and reopen the input file? Also, would there be a 'cleaner' way to do this program? I'm a novice to C programming and trying to improve. Thanks again – adohertyd Nov 30 '11 at 22:46
  • You had read until the EOF file already in the first `while` loop. The meant that when `fgets()` was called in the second `while` loop it would already be at the EOF. – hmjd Nov 30 '11 at 22:49
  • Ah I understand! I should have seen that. Thanks so much for your help. – adohertyd Nov 30 '11 at 22:51
-3

you used in this: _ fprintf(fp2, newcontent);_

and the signature of "fprintf" is int fprintf(FILE *stream, const char *format, ...); u miss the