Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C语言逐行读取文件_C - Fatal编程技术网

使用C语言逐行读取文件

使用C语言逐行读取文件,c,C,我正在编写一个程序,它逐行读取txt文件,并将每一行复制到一个空文本文件中。首先,程序读取字符直到行尾,并将读取的所有字符存储在缓冲区中。之后,它将缓冲区写入文本文件,并重复此过程,直到到达文件末尾。我面临的问题是:程序只写空文件的第一行。你能帮我解决这个问题吗,我是c语言的新手 我要读取的此内容txt文件: hey everyone how are you goood 这是该方案的产出: 大家好 这是源代码: #include <stdio.h> #include <

我正在编写一个程序,它逐行读取txt文件,并将每一行复制到一个空文本文件中。首先,程序读取字符直到行尾,并将读取的所有字符存储在缓冲区中。之后,它将缓冲区写入文本文件,并重复此过程,直到到达文件末尾。我面临的问题是:程序只写空文件的第一行。你能帮我解决这个问题吗,我是c语言的新手

我要读取的此内容txt文件:

hey everyone
how are you
goood    
这是该方案的产出: 大家好

这是源代码:

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int GetFileLenght(FILE*FP);

int main()
{
    FILE *SourceFile;
    FILE *DestinationFile;
    int c;
    int i=-1;
    int x=1;
    char buffer[350];
    int lenght;

    lenght=GetFileLenght(SourceFile);
    printf("%d",lenght);
    SourceFile=fopen("D:/SourceFile.txt","r");

    while(i!=lenght)
    {
        c=fgetc(SourceFile);
        printf("%c",c);

        switch(c)
        {
        case '\n' :
            DestinationFile=fopen("d:/des.txt","a+");
            fputs(buffer,DestinationFile);
            *buffer=0;
            fclose(DestinationFile);

            break;

        default:
            i++;
            buffer[i]=c;
            printf("%s",buffer);
        }
    }
    return 0;
}

int GetFileLenght(FILE*FP)
{
    FP = fopen("d:/SourceFile.txt", "a+");
    fseek(FP, 0, SEEK_END);
    int lengthOfFile = ftell(FP);
    fclose(FP);
    return lengthOfFile;
}
#包括
#包括
#包括
int GetFileLenght(文件*FP);
int main()
{
文件*源文件;
文件*DestinationFile;
INTC;
int i=-1;
int x=1;
字符缓冲区[350];
内部长度;
lenght=GetFileLenght(源文件);
printf(“%d”,长度);
SourceFile=fopen(“D:/SourceFile.txt”、“r”);
while(i!=长度)
{
c=fgetc(源文件);
printf(“%c”,c);
开关(c)
{
案例“\n”:
DestinationFile=fopen(“d:/des.txt”,“a+”;
FPUT(缓冲区、目标文件);
*缓冲区=0;
fclose(DestinationFile);
打破
违约:
i++;
缓冲区[i]=c;
printf(“%s”,缓冲区);
}
}
返回0;
}
int GetFileLenght(文件*FP)
{
FP=fopen(“d:/SourceFile.txt”、“a+”);
fseek(FP,0,SEEK_END);
int lengthOfFile=ftell(FP);
fclose(FP);
返回长度窗口;
}
编辑: 我对程序做了一些更改,现在很好:

我还想知道是否有其他方法可以使用fputs向文件添加新行

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


int main()
{

FILE *SourceFile;
FILE *DestinationFile;
int c=0;
int i=-1;
int x=1;

char buffer[350];



SourceFile=fopen("D:/SourceFile.txt","r");
DestinationFile=fopen("d:/des.txt","a+");

while(x)
{
    c=fgetc(SourceFile);




    switch(c)
    {


    case '\n' :

        fputs(buffer,DestinationFile);
        fprintf(DestinationFile,"\n");
        printf("%s",buffer);
        strncpy(buffer, "", sizeof(buffer));

        i=-1 ;


        break;

    case  EOF :
        fputs(buffer,DestinationFile);
        x=0;

    default:
        i++;
        buffer[i]=c;

    }
}

fclose(DestinationFile);


return 0;
}
#包括
#包括
#包括
int main()
{
文件*源文件;
文件*DestinationFile;
int c=0;
int i=-1;
int x=1;
字符缓冲区[350];
SourceFile=fopen(“D:/SourceFile.txt”、“r”);
DestinationFile=fopen(“d:/des.txt”,“a+”;
while(x)
{
c=fgetc(源文件);
开关(c)
{
案例“\n”:
FPUT(缓冲区、目标文件);
fprintf(目标文件“\n”);
printf(“%s”,缓冲区);
strncpy(缓冲区“”,sizeof(缓冲区));
i=-1;
打破
案例EOF:
FPUT(缓冲区、目标文件);
x=0;
违约:
i++;
缓冲区[i]=c;
}
}
fclose(DestinationFile);
返回0;
}

缩进不好,不容易阅读。请修复。
i++;缓冲区[i]=c
您不会在每行之后重置
i
,但您的写入代码总是从bufer
fputs(缓冲区,DestinationFile)的开始写入。谢谢kaylum,我并没有真正注意到我的程序在修改了ebad缩进后工作得很好,不容易阅读。请修复。
i++;缓冲区[i]=c
您不会在每行之后重置
i
,但您的写入代码总是从bufer
fputs(缓冲区,DestinationFile)的开始写入。谢谢kaylum,我没有真正注意到我的程序在我所做的更改后现在运行得很好