64,662
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[])
{
string strFileName;
cin>>strFileName;
ifstream file(strFileName.c_str(),ios::in);
if(!file)
return 0;
string line;
vector<vector<string>*>* array=new vector<vector<string>*>;
while(getline(file,line,'\n'))
{
vector<string>* vecTmp=new vector<string>;
int pos=0;
int prev_pos=0;
while((pos=line.find_first_of(',',pos))!=string::npos)
{
vecTmp->push_back(line.substr(prev_pos,pos));
cout<<line.substr(prev_pos,pos)<<endl;
prev_pos=++pos;
}
array->push_back(vecTmp);
}
printf("Hello World!\n");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define LARGEST_ROW 3
#define LARGEST_COL 4
int main()
{
float data[LARGEST_ROW][LARGEST_COL];
FILE* fp;
float temp;
char value[20];
int i , j ,k ;
int c;
i = j = k = 0;
if((fp = fopen("/home/syz/example/data.txt","r")) == NULL)
{
printf("Open the file fail!\n");
return -1;
}
while((c = fgetc(fp)) != EOF)
{
switch(c)
{
case '\n':
value[k] = '\0';
temp = atof(value);
data[i][j] = temp;
k = 0;
i++;
j = 0;
break;
case ' ': //为了去掉逗号前后可能的空格
break;
case ',':
value[k] = '\0';
temp = atof(value);
data[i][j] = temp;
k = 0;
j++;
break;
default:
value[k] = (char)c;
k++;
}
}
for( i = 0; i < LARGEST_ROW; i++)
{
for( j = 0; j <LARGEST_COL; j++)
{
printf("%.4f\t", data[i][j]);
}
printf("\n");
}
fclose(fp);
return 0;
}