c语言怎么从TXT文件中读取小数数据啊? 50
4个回答
展开全部
/*我的TXT文件是小数文件,每个数据之间用回车分隔,请问怎样才能将它读入我的程序啊,紧急求教!*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define FILE_PATH "test.txt"
#define MAX_LINE 100
void main()
{
FILE* fileName = NULL;
int fileLen = 0;
char* buffer = NULL;
char* array[MAX_LINE];
char* temp = NULL;
int lineCount = 0;
int preNumLen = 0;
fileName = fopen(FILE_PATH, "r");
if(fileName == NULL)
{
printf("无法打开指定的文件, 请确定文件是否存在\n");
return;
}
fseek(fileName, 0, SEEK_END);
fileLen = ftell(fileName);
fseek(fileName, 0, SEEK_SET);
buffer = (char*)malloc((fileLen + 1) * sizeof(char));
if(buffer == NULL)
return;
memset(buffer, '\0', (fileLen + 1) * sizeof(char));
fread(buffer, sizeof(char), fileLen, fileName);
for(temp = strtok(buffer, "\n\r"); temp; temp = strtok(NULL, "\n\r"))
{
preNumLen = strlen(temp);
array[lineCount] = (char*)malloc((preNumLen + 1) * sizeof(char));
memset(array[lineCount], '\0', (preNumLen + 1) * sizeof(char));
strcpy(array[lineCount], temp);
lineCount++;
}
printf("文件中的数据是: \n");
for(int i=0; i<lineCount; i++)
{
printf("%s\n", array[i]);
free(array[i]);
array[i] = NULL;
}
free(buffer);
buffer = NULL;
}
/*文件名字我起的叫"text.txt",你可以自己起个, 记住, 在运行程序之前务必保证当前目录下有这个文件, 因为"r"的方式当没有文件时会发生错误, 文件的存储格式为:
1.2222
2.2222
3.2222
*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#define FILE_PATH "test.txt"
#define MAX_LINE 100
void main()
{
FILE* fileName = NULL;
int fileLen = 0;
char* buffer = NULL;
char* array[MAX_LINE];
char* temp = NULL;
int lineCount = 0;
int preNumLen = 0;
fileName = fopen(FILE_PATH, "r");
if(fileName == NULL)
{
printf("无法打开指定的文件, 请确定文件是否存在\n");
return;
}
fseek(fileName, 0, SEEK_END);
fileLen = ftell(fileName);
fseek(fileName, 0, SEEK_SET);
buffer = (char*)malloc((fileLen + 1) * sizeof(char));
if(buffer == NULL)
return;
memset(buffer, '\0', (fileLen + 1) * sizeof(char));
fread(buffer, sizeof(char), fileLen, fileName);
for(temp = strtok(buffer, "\n\r"); temp; temp = strtok(NULL, "\n\r"))
{
preNumLen = strlen(temp);
array[lineCount] = (char*)malloc((preNumLen + 1) * sizeof(char));
memset(array[lineCount], '\0', (preNumLen + 1) * sizeof(char));
strcpy(array[lineCount], temp);
lineCount++;
}
printf("文件中的数据是: \n");
for(int i=0; i<lineCount; i++)
{
printf("%s\n", array[i]);
free(array[i]);
array[i] = NULL;
}
free(buffer);
buffer = NULL;
}
/*文件名字我起的叫"text.txt",你可以自己起个, 记住, 在运行程序之前务必保证当前目录下有这个文件, 因为"r"的方式当没有文件时会发生错误, 文件的存储格式为:
1.2222
2.2222
3.2222
*/
展开全部
/*我的TXT文件是小数文件,每个数据之间用回车分隔,请问怎样才能将它读入我的程序啊,紧急求教!*/
#include
<stdio.h>
#include
<malloc.h>
#include
<string.h>
#define
FILE_PATH
"test.txt"
#define
MAX_LINE
100
void
main()
{
FILE*
fileName
=
NULL;
int
fileLen
=
0;
char*
buffer
=
NULL;
char*
array[MAX_LINE];
char*
temp
=
NULL;
int
lineCount
=
0;
int
preNumLen
=
0;
fileName
=
fopen(FILE_PATH,
"r");
if(fileName
==
NULL)
{
printf("无法打开指定的文件,
请确定文件是否存在\n");
return;
}
fseek(fileName,
0,
SEEK_END);
fileLen
=
ftell(fileName);
fseek(fileName,
0,
SEEK_SET);
buffer
=
(char*)malloc((fileLen
+
1)
*
sizeof(char));
if(buffer
==
NULL)
return;
memset(buffer,
'\0',
(fileLen
+
1)
*
sizeof(char));
fread(buffer,
sizeof(char),
fileLen,
fileName);
for(temp
=
strtok(buffer,
"\n\r");
temp;
temp
=
strtok(NULL,
"\n\r"))
{
preNumLen
=
strlen(temp);
array[lineCount]
=
(char*)malloc((preNumLen
+
1)
*
sizeof(char));
memset(array[lineCount],
'\0',
(preNumLen
+
1)
*
sizeof(char));
strcpy(array[lineCount],
temp);
lineCount++;
}
printf("文件中的数据是:
\n");
for(int
i=0;
i<lineCount;
i++)
{
printf("%s\n",
array[i]);
free(array[i]);
array[i]
=
NULL;
}
free(buffer);
buffer
=
NULL;
}
/*文件名字我起的叫"text.txt",你可以自己起个,
记住,
在运行程序之前务必保证当前目录下有这个文件,
因为"r"的方式当没有文件时会发生错误,
文件的存储格式为:
1.2222
2.2222
3.2222
*/
#include
<stdio.h>
#include
<malloc.h>
#include
<string.h>
#define
FILE_PATH
"test.txt"
#define
MAX_LINE
100
void
main()
{
FILE*
fileName
=
NULL;
int
fileLen
=
0;
char*
buffer
=
NULL;
char*
array[MAX_LINE];
char*
temp
=
NULL;
int
lineCount
=
0;
int
preNumLen
=
0;
fileName
=
fopen(FILE_PATH,
"r");
if(fileName
==
NULL)
{
printf("无法打开指定的文件,
请确定文件是否存在\n");
return;
}
fseek(fileName,
0,
SEEK_END);
fileLen
=
ftell(fileName);
fseek(fileName,
0,
SEEK_SET);
buffer
=
(char*)malloc((fileLen
+
1)
*
sizeof(char));
if(buffer
==
NULL)
return;
memset(buffer,
'\0',
(fileLen
+
1)
*
sizeof(char));
fread(buffer,
sizeof(char),
fileLen,
fileName);
for(temp
=
strtok(buffer,
"\n\r");
temp;
temp
=
strtok(NULL,
"\n\r"))
{
preNumLen
=
strlen(temp);
array[lineCount]
=
(char*)malloc((preNumLen
+
1)
*
sizeof(char));
memset(array[lineCount],
'\0',
(preNumLen
+
1)
*
sizeof(char));
strcpy(array[lineCount],
temp);
lineCount++;
}
printf("文件中的数据是:
\n");
for(int
i=0;
i<lineCount;
i++)
{
printf("%s\n",
array[i]);
free(array[i]);
array[i]
=
NULL;
}
free(buffer);
buffer
=
NULL;
}
/*文件名字我起的叫"text.txt",你可以自己起个,
记住,
在运行程序之前务必保证当前目录下有这个文件,
因为"r"的方式当没有文件时会发生错误,
文件的存储格式为:
1.2222
2.2222
3.2222
*/
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
按行读取文本文件你会吧? 按照文本来读即可, 每一行读取以后, 做一个字符串到浮点数的转换.
假设读取进来的字符串是 strLine, 转换部分代码如下:
float fNumber = atof( strLine );
假设读取进来的字符串是 strLine, 转换部分代码如下:
float fNumber = atof( strLine );
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
int main()
{
FILE *fp;
float tmp;
fp=fopen("abc.txt","r");
while(fscanf(fp,"%f",&tmp)!=EOF)
printf("%f\n",tmp);
fclose(fp);
}
int main()
{
FILE *fp;
float tmp;
fp=fopen("abc.txt","r");
while(fscanf(fp,"%f",&tmp)!=EOF)
printf("%f\n",tmp);
fclose(fp);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询