简体   繁体   中英

How to read from file and write another in c

I have a input file like this:

This is 1nd Line.
This is 2nd Line.
This 3rd Line.

And I need to output files like

OddLines.txt:

This is 1nd Line.
This 3rd Line.

EvenLines.txt:

This is 2nd Line.

Here is my code. And not working as I wanted.

    char buf[256];
    int ch;
    int lines;
    lines = 1;

    FILE *myFile = fopen(argv[1], "r");

    if (myFile == NULL) {
        printf("Open error \n");
        exit(-1);
    }

    FILE *outFile = fopen("oddlines.txt", "w");
    FILE *outFile1 = fopen("evenlines.txt", "w");

    while (fgets(buf, sizeof(buf), myFile) != NULL) {
        if (ch == '\n')
            lines++;
        else
        if ((lines % 2) == 0)
            fputs(buf, outFile1);
        else
            fputs(buf, outFile);
    }
    fclose(myFile);
    fclose(outFile);
    fclose(outFile1);
}
if (ch == '\n')

This code does not make sense. Do you want to find enter character? If YES, you can use strcspn function:

int pos = strcspn ( buf, "\n" );

The lines should be initialize by 0 not 1 :

    int lines = 0;
    while(fgets(buf, sizeof(buf),myFile))
    {
        lines++;
        int pos = strcspn ( buf, "\n" ); // find the enter character
        buf[pos] = ' '; // add space at the end of string
        buf[pos+1] = '\0';
        if ((lines%2)==0) // if even
            fputs (buf,outFile1); 
        else               // if odd
            fputs (buf,outFile);
    }

You should check the fopen function:

FILE *myFile = fopen(argv[1], "r");
if(!myFile) {return -1;}

FILE *outFile = fopen("oddlines.txt", "w");
if(!outFile) {return -1;}
FILE *outFile1 = fopen("evenlines.txt", "w");
if(!outFile1) {return -1;}

And the condition of command line:

if(argc < 2 ) {
    printf("usage: %s <input_filename>", argv[0]);
    return-1;
}

The complete code:

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

int main(int argc, char const *argv[]) {

    char buf[256];
    int lines = 0;

    if(argc < 2 ) {
        printf("usage: %s <input_filename>", argv[0]);
        return-1;
    }

    FILE *myFile = fopen(argv[1], "r");
    if(!myFile) {return -1;}
    FILE *outFile = fopen("oddlines.txt", "w");
    if(!outFile) {return -1;}
    FILE *outFile1 = fopen("evenlines.txt", "w");
    if(!outFile1) {return -1;}

    while(fgets(buf, sizeof(buf),myFile)!=NULL)
    {
        lines++;
        int pos = strcspn ( buf, "\n" );
        buf[pos] = ' ';
        buf[pos+1] = '\0';
        if ((lines%2)==0)
            fputs (buf,outFile1);
        else
            fputs (buf,outFile);
    }

    fclose(myFile);
    fclose(outFile);
    fclose(outFile1);
}

the input file:

This is 1nd Line.
This is 2nd Line.
This is 3rd Line.
This is 4th Line.
This is 5th Line.
This is 6th Line.

The output file:

$cat evenlines.txt

This is 2nd Line. This is 4th Line. This is 6th Line. 

$cat oddlines.txt

This is 1nd Line. This is 3rd Line. This is 5th Line. 
if (ch == '\n')

The problem was in this line.

#include <stdio.h>
#include<string.h>
void main() 
{
    int line=1;
    char buf[256];

    FILE *fp, *fp_Odd, *fp_Even;
    fp=fopen("USERS.TXT", "r");

    fp_Odd=fopen("ODD.TXT", "a");
    fp_Even=fopen("EVEN.TXT", "a");

    if(fp == NULL)
    {
        printf("Open error \n");
        exit(-1);
    }

    while(fgets(buf, sizeof(buf), fp)!=NULL)
    {
        if(strcmp(buf, "\n")==0)
        line++;

        else if(line%2 != 0)
            fprintf(fp_Odd, "%s", buf);
        else
            fprintf(fp_Even, "%s", buf);

        line++;
    }
    fclose(fp);
}

You can check this code. I have made the necessary changes. Hope you understand.

It is unclear whether you want the output files to contains the lines from the input file or if these lines must be concatenated, stripping the newlines.

You code does not work because the test if (ch == '\n') has undefined behavior, as ch is uninitialized. Hence the line counter is not properly updated and all lines go into one of the files.

Counting the lines is actually not as simpler as counting the iterations of the loop as some lines in the input file might be longer than the length of the array used by fgets() . You should update the line counter after writing by testing if the line just written actually contained a newline character.

Here is a modified version:

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

int main(int argc, char *argv[]) {
    char buf[256];
    int lines = 1;
    FILE *myFile, *outFile, *outFile1;

    if (argc < 2) {
        printf("Missing argument\n");
        return 1;
    }
    if ((myFile = fopen(argv[1], "r")) == NULL) {
        printf("Open error for %s\n", argv[1]);
        return 1;
    }
    if ((outFile = fopen("oddlines.txt", "w")) == NULL) {
        printf("Open error for %s\n", "oddlines.txt");
        return 1;
    }
    if ((outFile1 = fopen("evenlines.txt", "w")) == NULL) {
        printf("Open error for %s\n", "evenlines.txt");
        return 1;
    }

    while (fgets(buf, sizeof(buf), myFile) != NULL) {
        if (lines % 2 == 0)
            fputs(buf, outFile1);
        else
            fputs(buf, outFile);

        /* if a full line was read, increment the line number */
        if (strchr(buf, '\n')
            lines++;
    }
    fclose(myFile);
    fclose(outFile);
    fclose(outFile1);
    return 0;
}

Here is a simpler version that does not use fgets() :

#include <stdio.h>

int main(int argc, char *argv[]) {
    int c, out;
    FILE *myFile, *outFile[2];

    if (argc < 2) {
        printf("Missing argument\n");
        return 1;
    }
    if ((myFile = fopen(argv[1], "r")) == NULL) {
        printf("Open error for %s\n", argv[1]);
        return 1;
    }
    if ((outFile[0] = fopen("oddlines.txt", "w")) == NULL) {
        printf("Open error for %s\n", "oddlines.txt");
        return 1;
    }
    if ((outFile[1] = fopen("evenlines.txt", "w")) == NULL) {
        printf("Open error for %s\n", "evenlines.txt");
        return 1;
    }

    out = 0; /* current output file is "oddlines.txt" */
    while ((c = getc(myFile)) != EOF) {
        putc(c, outFile[out]);
        if (c == '\n')
            out = 1 - out;  /* change current output file */
    }
    fclose(myFile);
    fclose(outFile[0]);
    fclose(outFile[1]);
    return 0;
}

If you wish to strip vowels from a third output file, just include <string.h> , open that file to FILE *noVowelFile and add a test in the while loop:

    out = 0; /* current output file is "oddlines.txt" */
    while ((c = getc(myFile)) != EOF) {
        if (strchr("aeiouAEIOU", c) == NULL) {
            putc(c, noVowelFile);
        }
        putc(c, outFile[out]);
        if (c == '\n')
            out = 1 - out;  /* change current output file */
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM