Linux에서 C/C++는 txt 파일, 여러 줄 파일을 읽고 각 줄을 쉼표로 구분합니다.

소스 코드

test.cpp

#include<string>
#include<iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main()
{
    
    
	FILE *fd;
	long dev;
	long offset;
	long length;
	char ch;
	double ts = 0.000000;
	if ((fd = fopen("bbb.txt", "r"))<0)
	{
    
    
		printf("open the file is error!\n");
		exit(0);
	}
	fseek(fd, 0, SEEK_SET);
	while (!feof(fd)) 
	{
    
    
		fscanf(fd, "%ld,%ld,%ld,%c,%lf\n", &dev, &offset, &length, &ch, &ts);
		printf("%ld,%ld,%ld,%c,%lf\n", dev, offset, length, ch, ts);
	}
	fclose(fd);
	return 0;
}

테스트 파일: bbb.txt

2,50,41,w,20.585828
4,52,51,r,52.012547

엮다

sudo g++ test.cpp 

효과

여기에 이미지 설명 삽입

추천

출처blog.csdn.net/mao_hui_fei/article/details/122862960