To read TXT files in C language, requirements: read by line, and then output to the specified TXT file by line

To read TXT files in C language, requirements: read by line, and then output to the specified TXT file by line

A review:
the problems encountered and the needs of the situation:

  • There are 629 pieces of lightning disaster data in Beijing from 2002 to 2013 obtained from the Beijing Lightning Disaster Detection website. In order to perform text clustering, 629 pieces of data in a txt file need to be scattered into 629 txt files , Manual classification will take at least 5 hours. In order to save time, choose C language to divide 629 data into 629 txt files to prepare data for future text clustering.
    Since I haven't read the C language for a long time, I have forgotten the relevant file input and output functions, so I can take this opportunity to review the relevant knowledge.

The main functions used :

  • fopen
  • feof
  • fgets
  • fputs
  • fclsoe

In order to realize the self-increment operation of the output file name, the sprintf function is used as a string formatting command. There are indeed a lot of pits in the middle. Please attach the code in the form of a blog to record it!
Time is relatively short, so the related optimization and beautification work of the code is not done!

code show as below

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

char * ReadSpeacialLine(int i)
{
    
    
    char filename[] = "C:/Users/zhangbin/Desktop/tet/test.txt"; //文件名
    FILE *fp;
    int WhichLine=i;             //指定要读取哪一行
    int CurrentIndex=0;             //当前读取的行
    static char StrLine[1024];             //每行最大读取的字符数,可根据实际情况扩大
    if((fp = fopen(filename,"r")) == NULL) //判断文件是否存在及可读
    {
    
    
        printf("error!");
        return NULL;
    }

    while (!feof(fp))
    {
    
    

        if (CurrentIndex==WhichLine)
        {
    
    
            fgets(StrLine,1024,fp);  //读取一行
            printf("%s", StrLine); //输出
            return StrLine;
        }
        fgets(StrLine,1024,fp);  //读取一行,并定位到下一行
        CurrentIndex++;

        //printf("%s", StrLine); //输出
    }
    fclose(fp);                     //关闭文件
    return NULL;
}

int main()
{
    
    
    FILE * fd;
    char * sBuf ;//= (char *)malloc(1024);
    char * fileName = "C:/Users/zhangbin/Desktop/tet/";
    char  sName[1024];
    //int num = 1;
    //sprintf(sName,"%s%d.txt",fileName,num);
    //printf("%s",sName);
    //输出位置
    for(int i = 0; i < 629; i++)
    {
    
    
        int num = i + 1;
        sBuf = ReadSpeacialLine(i);
        printf("%s",sBuf);
        sprintf(sName,"%s%d.txt",fileName,num);   //字符串格式化输出
        printf("%s",sName);
        fd = fopen(sName,"w");
        fputs(sBuf,fd);
        fclose(fd);
    }

   // free(sBuf);
    return 0;
}

Original data:
Insert picture description here
The effect after extracting by line is as follows:
Insert picture description here
Each txt file has similar content as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41782149/article/details/104593812