#include <string.h>
char *strtok( char *str1, const char *str2 );
就能解決。
先用getline()壹行讀出壹個str,並累加行數,然後
char *result = NULL;
char string[100][100];
int x = 0;
result = strtok( str, " ");
while( result != NULL ) {
strcpy(string[x++] , result);
result = strtok( NULL, " " );
}
這樣用壹個string數組就可以把全部數據保存下來。x記錄了總數,然後x除以行數就是列數。
今天有空了,幫妳把程序全寫出來:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void main(){
char ch[100] = "\\0";
char b[100][100];
int x=0,y=0;
ifstream fin("123.txt",ios::in);
fin.getline(ch,100);
while(!fin.eof()){
++x;
cout<<ch<<endl;
char *result = NULL;
result = strtok(ch,",");
while( result != NULL ) {
strcpy(b[y++],result);
cout<<b[y-1]<<endl;
result = strtok( NULL, "," );
}
memset(ch,0,100);
fin.getline(ch,100);
}
fin.close();
}
//已運行過了,沒問題,b[100][100]是所有元素,x為行數,y/x為列數。