64,646
社区成员
发帖
与我相关
我的任务
分享
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
const int max = 100;
int main()
{
using namespace std;
ifstream data("data.txt");
int x, y, num[max][max];
for (x = 0; !data.eof(); x++)
{
string line;
getline(data, line);
istringstream ss(line);
for (y = 0; !ss.eof(); y++)
{
ss >> num[x][y];
}
}
return 0;
}
#include <stdio.h>
#include <sstream>
using namespace std;
#define max 100
void main()
{
FILE* fp = fopen("data.txt", "r");
if (!fp)
{
return;
}
int num[max][max] = {0};
int i = 0, j = 0;
while(!feof(fp))
{
char str[1024] = {0};
fgets(str, 1024, fp);
str[strlen(str)] = '\0';
istringstream is(str);
string s;
while(std::getline(is, s, ' '))
{
num[i][j] = atoi(s.c_str());
j++;
}
i++;
}
for (int m = 0; m < max; m++)
{
bool bHasNum = false;
for (int n = 0; n < max; n++)
{
if (num[m][n] != 0)
{
bHasNum = true;
printf("%d ", num[m][n]);
}
}
if (bHasNum)
{
printf("\n");
}
}
getchar();
}