如何从文件中,如:txt文本文件 读入浮点数并且存入浮点数组。

Spiritring 2007-08-13 12:52:18
如何从文件中。如:txt文本文件 读入浮点数并且存入浮点数组。
这是做的字符load的程序。
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

using namespace std;

// --- Script -----------------------------------------------------------------------------

char ** g_ppstrScript = NULL; // Pointer to the current script
int g_iScriptSize; // The size of the current script
int g_iCurrScriptLine; // The current line in the script
int g_iCurrScriptLineChar; // The current character in the current

// ---- Source Code -----------------------------------------------------------------------

#define MAX_SOURCE_LINE_SIZE 4096 // Maximum source line length
#define MAX_COMMAND_SIZE 64 // Maximum length of a command
#define MAX_PARAM_SIZE 1024 // Maximum length of a parameter

// ---- Command Names ---------------------------------------------------------------------
main()
{
FILE * pScriptFile;
if ( ! ( pScriptFile = fopen ( "B", "r" ) ) )
{
printf ( "File I/O error.\n" );
exit ( 0 );
}
while ( ! feof ( pScriptFile ) )
if ( fgetc ( pScriptFile ) == '\n' )
++ g_iScriptSize;
++ g_iScriptSize;


if ( ! ( pScriptFile = fopen ( "B", "r" ) ) )
{
printf ( "File I/O error.\n" );
exit ( 0 );
}

g_ppstrScript = ( char ** ) malloc ( g_iScriptSize * sizeof ( char * ) );

for ( int iCurrLineIndex = 0; iCurrLineIndex < g_iScriptSize; ++ iCurrLineIndex )
{
g_ppstrScript [ iCurrLineIndex ] = ( char * ) malloc ( MAX_SOURCE_LINE_SIZE + 1 );
fgets ( g_ppstrScript [ iCurrLineIndex ], MAX_SOURCE_LINE_SIZE, pScriptFile );
}
fclose ( pScriptFile );

for ( int iCurrLineIndex = 0; iCurrLineIndex < g_iScriptSize; ++ iCurrLineIndex )
{
cout<<g_ppstrScript [ iCurrLineIndex ];
}
system("pause");
}
问题是如何读入浮点数,放入浮点数组呢?
哪位大侠帮忙。谢谢。
...全文
959 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
琅琊榜 2007-08-14
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
ifstream ifile("B");
if (!ifile) {
cerr << "couldn't open file B" << endl;
return 1;
}

vector<float> array;
float f;
while (!ifile.eof()) {
ifile >> f;
array.push_back(f);
}
// now array is the float array you want.
return 0;
}
Spiritring 2007-08-13
  • 打赏
  • 举报
回复
补充一下,一个float数,是由换行决定的,所以
B文件的内容是种形式:
12.3
23.4
5.67
2.67
56.789
星羽 2007-08-13
  • 打赏
  • 举报
回复

在你的基础上帮你改了

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

using namespace std;

// --- Script -----------------------------------------------------------------------------

char ** g_ppstrScript = NULL; // Pointer to the current script
int g_iScriptSize; // The size of the current script
int g_iCurrScriptLine; // The current line in the script
int g_iCurrScriptLineChar; // The current character in the current

// ---- Source Code -----------------------------------------------------------------------

#define MAX_SOURCE_LINE_SIZE 4096 // Maximum source line length
#define MAX_COMMAND_SIZE 64 // Maximum length of a command
#define MAX_PARAM_SIZE 1024 // Maximum length of a parameter

// ---- Command Names ---------------------------------------------------------------------
void main()
{
double* pData = 0;

FILE * pScriptFile;
if ( ! ( pScriptFile = fopen ( "B", "r" ) ) )
{
printf ( "File I/O error.\n" );
exit ( 0 );
}
while ( ! feof ( pScriptFile ) )
if ( fgetc ( pScriptFile ) == '\n' )
++ g_iScriptSize;
++ g_iScriptSize;


if ( ! ( pScriptFile = fopen ( "B", "r" ) ) )
{
printf ( "File I/O error.\n" );
exit ( 0 );
}

g_ppstrScript = ( char ** ) malloc ( g_iScriptSize * sizeof ( char * ) );

pData = (double*)malloc(g_iScriptSize * sizeof(double));

for ( int iCurrLineIndex = 0; iCurrLineIndex < g_iScriptSize; ++ iCurrLineIndex )
{
g_ppstrScript [ iCurrLineIndex ] = ( char * ) malloc ( MAX_SOURCE_LINE_SIZE + 1 );
fgets ( g_ppstrScript [ iCurrLineIndex ], MAX_SOURCE_LINE_SIZE, pScriptFile );
}
fclose ( pScriptFile );

for ( int iCurrLineIndex = 0; iCurrLineIndex < g_iScriptSize; ++ iCurrLineIndex )
{
//cout<<g_ppstrScript [ iCurrLineIndex ];
pData[iCurrLineIndex] = atof(g_ppstrScript[iCurrLineIndex]);
}

for ( int iCurrLineIndex = 0; iCurrLineIndex < g_iScriptSize; ++ iCurrLineIndex )
{
cout<<pData[iCurrLineIndex]<<endl;
}

system("pause");
}
  • 打赏
  • 举报
回复
#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
#include<fstream>
using namespace std;


int main(int , char* [])
{
ifstream fs("a.txt");
istream_iterator<double> iter( fs ),end;
vector<double> vd;
copy( iter , end ,back_inserter( vd ) );
copy( vd.begin(),vd.end() , ostream_iterator<double>(cout," ") );
return 0;
};
todototry 2007-08-13
  • 打赏
  • 举报
回复
直接fstream即可以
流对象>>fvar;
f[index] = fvar;即可以,okokok
奶糖人五号 2007-08-13
  • 打赏
  • 举报
回复
mark
jixingzhong 2007-08-13
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>

#define LEN 5
int main()
{
float arr[LEN];
int i;
FILE *fp = fopen("test.txt", "r");

for (i=0; i<LEN; i++)
fscanf(fp, "%f", arr+i);

printf("Read data(s):\n");
for (i=0; i<LEN; i++)
printf("%f\n", arr[i]);
return 0;
}

test.txt 文件为测试文件,在当前目录下:
12.3
23.4
5.67
2.67
56.789
jixingzhong 2007-08-13
  • 打赏
  • 举报
回复
fscanf
gfxiang 2007-08-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream ifile("B");
if (!ifile) {
cerr << "couldn't open file B" << endl;
return 1;
}

int max_size;
float *array;
const int more = 100; // 这个根据需要自己设置动态增加数组元素的个数
max_size = more;
array = (float*)malloc(max_size*sizeof(float));
int i=0;
while (!ifile.eof()) {
if (i==max_size) {
max_size += more;
array = (float*)realloc(array, size*sizeof(float));
}
ifile >> array[i++];
}
int size = i; // 浮点元素的个数
// ...

free(array);
return 0;
}
Spiritring 2007-08-13
  • 打赏
  • 举报
回复
哦,忘记说了,最好不使用模板类。
txt文本文件 读入浮点数并且存入浮点数组。
是这样的一个问题。
herman~~ 2007-08-13
  • 打赏
  • 举报
回复
MARK
gfxiang 2007-08-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
ifstream ifile("B");
if (!ifile) {
cerr << "couldn't open file B" << endl;
return 1;
}

vector<float> array;
float f;
while (!ifile.eof()) {
ifile >> f;
array.push_back(f);
}
// now array is the float array you want.
return 0;
}

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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