C语言按行读文件及字符串分割

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


int main()
{
	char s[50];
	char delims[] = " ";

	FILE *fs;
	fopen_s(&fs, "check-in.txt", "rt");
	if (fs == NULL)
	{
		printf("file open error
");
		return 0;
	}

	//while (fgets(s, 50, fs) != NULL)
	for (int i = 0; i < 10;i++)
	{
		printf("%d
", i);
		int j = 0;
		fgets(s, 50, fs);
		printf("%s", s);
		char result[6][10];
		char *t = NULL;
		char *rest = NULL;
		t = strtok_s(s, delims, &rest);
		for (j = 0; t != NULL && j < 6; j++)
		{
			strcpy_s(result[j],t);
			printf("--%s", result[j]);
			t = strtok_s(NULL, delims, &rest);
		}
		printf("
");
	}

	fclose(fs);
	return 0;
}

  

原文地址:https://www.cnblogs.com/argenbarbie/p/4604096.html