请教:如何判断.txt文件中一行有几个数字(数字之间用空格分开)以及总共有几行??

bladezh 2004-04-22 04:53:36
如题,另外如何将每行的数字存入一个数组中?由于在编写代码时并不知道一行有几个数字,所以好像不能用sscanf(str,"%f %f",&m_a,&m_b)这样的方式实现吧,小弟被此问题困扰4天有余,还请前辈高人们不吝赐教。
...全文
313 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinxuliang 2004-04-23
  • 打赏
  • 举报
回复
不用那么麻烦的用这个函数,可以解决了
CString::SpanExcluding
CString SpanExcluding( LPCTSTR lpszCharSet ) const;
throw( CMemoryException );

Return Value

A substring that contains characters in the string that are not in lpszCharSet, beginning with the first character in the string and ending with the first character found in the string that is also in lpszCharSet (that is, starting with the first character in the string and up to but excluding the first character in the string that is found lpszCharSet). It returns the entire string if no character in lpszCharSet is found in the string.

Parameters

lpszCharSet

A string interpreted as a set of characters.

Remarks

Use this function to search the string for the first occurrence of any character in the specified set lpszCharSet. SpanExcluding extracts and returns all characters preceding the first occurrence of a character from lpszCharSet (in other words, the character from lpszCharSet and all characters following it in the string, are not returned). If no character from lpszCharSet is found in the string, then SpanExcluding returns the entire string.

Example

The following function returns the first portion of the src param.

// Portions are delimited by a semi-colon( ; ),
// a comma( , ), a period( . ), a dash( - ),
// or a colon( : ).

CString GetFirstPart( CString src)
{
return src.SpanExcluding( ";,.- :");
}

wenxi2004 2004-04-23
  • 打赏
  • 举报
回复
mark
rabo 2004-04-23
  • 打赏
  • 举报
回复
我来说个土办法
CString str;
str.Mid(1,str.Find(" ",0));
循环计数就行了。
bladezh 2004-04-22
  • 打赏
  • 举报
回复
joise兄,我对vc不是很熟,你可否讲的详细一点?能给出代码最好了,谢谢!
grasshopperliu 2004-04-22
  • 打赏
  • 举报
回复
首先,你要读取你的所有行,
CFile thisfile;

//打开文件
thisfile.Open("yourfilename",CFile::modeRead);
DWORD fileLength=thisfile.GetLength();
char* TheFile = new char[fileLength]; //动态分配数组
//按字符方式读入文件
thisfile.Read(TheFile,fileLength);
thisfile.Close();
然后
char blank=' ';
CString mystr[10000]; //假设的最大读入字符串个数10000
long int number=0; //真实的字符串个数
//读入字符串过程
for(long int i=0;i<(long)fileLength;i++)

{

if(TheFile[i]!=blank)
mystr[number]=mystr[number]+TheFile[i];

else {

if((heFile[i+1]!=blank)
number++;}

}
这样你的数据就都保存在字符串里了,一个字符串一个,VC中有一个函数,
不好意思我忘了它的名字,就是将字符串中的数字给变回到数字类型,你可以再找知道的问一下。
legend-never-die 2004-04-22
  • 打赏
  • 举报
回复
首先,用getline一行一行读
其次,每行读的时候用getline,隔开符选择' ',就可以得到有几个数字,也可以得到有几行了

将每行数字读入数组更简单了,把每个读到的数字用atoi或atof转化成int存入一个vector即可

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧