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
今日推荐