簡體   English   中英

如何在 c 中讀取文件並寫入另一個文件

[英]How to read from file and write another in c

我有一個這樣的輸入文件:

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

我需要 output 文件,例如

奇數行.txt:

This is 1nd Line.
This 3rd Line.

EvenLines.txt:

This is 2nd Line.

這是我的代碼。 並且沒有按我的意願工作。

    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')

這段代碼沒有意義。 你想找到輸入字符嗎? 如果是,您可以使用strcspn function:

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

這些lines應該由0而不是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);
    }

您應該檢查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;}

以及命令行的條件:

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

完整代碼:

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

輸入文件:

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

output 文件:

$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')

問題出在這一行。

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

您可以檢查此代碼。 我已經進行了必要的更改。 希望你能理解。

目前尚不清楚您是否希望 output 文件包含輸入文件中的行,或者是否必須連接這些行,從而去除換行符。

您的代碼不起作用,因為測試if (ch == '\n')具有未定義的行為,因為ch未初始化。 因此,行計數器未正確更新,所有行 go 到其中一個文件中。

計算行數實際上並不像計算循環的迭代次數那么簡單,因為輸入文件中的某些行可能比fgets()使用的數組的長度長。 您應該在寫入后通過測試剛剛寫入的行是否實際包含換行符來更新行計數器。

這是修改后的版本:

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

這是一個不使用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;
}

如果您想從第三個 output 文件中去除元音,只需包含<string.h> ,將該文件打開到FILE *noVowelFile並在while循環中添加一個測試:

    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 */
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM