當前位置:編程學習大全網 - 源碼下載 - MFC中讀取TXT文件中的數據和行列數

MFC中讀取TXT文件中的數據和行列數

既然有1位數的數據也有4位數的數據,那麽如果txt中沒有特意的回車,行數和列數不可能確定。如果有回車的話,簡單,用

#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為列數。

  • 上一篇:《笑看風雲-歸途》天華CP-第三十四章(完)
  • 下一篇:天上星星轉,地上窗簾垂,是什麽意思?
  • copyright 2024編程學習大全網