用c语言读取文件

w372011482 2011-10-06 04:35:06
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;

const int MAX_ARRAY_SIZE = 10;
void fillArray(double a[], int size, int& numberUsed);
void showArray(const double a[], int numberUsed);

int _tmain(int argc, _TCHAR* argv[])
{
double dblArray[MAX_ARRAY_SIZE];
int numberUsed;
cout << "\n\t请选择需要打开的文件名:\n\n";

fillArray(dblArray, MAX_ARRAY_SIZE, numberUsed);
showArray(dblArray, numberUsed);

cout << endl << "请按任意键继续...";
getch();
return 0;
}

void fillArray(double a[], int size, int& numberUsed)
{
ifstream inStream; // To read in file
char fileName[20]; // name of file
cout << "\n\nPlease enter a file name from the following: \n\n";
system("dir /B *.txt"); // list text files in directory

cout << "\n\nPlease enter a file name: ";
cin >> fileName; // Read in the file name
cout << endl << endl ;
inStream.open(fileName);

int index = 0;
numberUsed = 0;
while ( ! inStream.eof() && index < size )
{
inStream >> a[index];
index++;
}
inStream.close();
numberUsed = index;
}

void showArray(const double a[], int numberUsed)
{
cout << "Array has the following values: \n\n";

cout << "\tIndex\tValue\n";
for (int index = 0; index < numberUsed; index++)
{
cout << "\t" << index << "\t"
<< a[index] << endl;

}
}
谁能把这个程序改成c语言版的?我失败了。
...全文
75 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
浮尘grass 2011-10-06
  • 打赏
  • 举报
回复

//#include <tchar.h>
//#include <iostream>
//#include <fstream>
//#include <conio.h>
//using namespace std;
#include <stdio.h>
#include <stdlib.h>
const int MAX_ARRAY_SIZE = 10;
void fillArray(float a[], int size, int& numberUsed);
void showArray(const float a[], int numberUsed);

int main()
{
float dblArray[MAX_ARRAY_SIZE];
int numberUsed;
//cout << "\n\t请选择需要打开的文件名:\n\n";
printf("\n\t请选择需要打开的文件名:\n\n");

fillArray(dblArray, MAX_ARRAY_SIZE, numberUsed);
showArray(dblArray, numberUsed);

//cout << endl << "请按任意键继续...";
printf("请按任意键继续...");
//getch();
return 0;
}

void fillArray(float a[], int size, int& numberUsed)
{
FILE *fp;
// ifstream inStream; // To read in file
char fileName[20]; // name of file
//cout << "\n\nPlease enter a file name from the following: \n\n";
printf("\n\nPlease enter a file name from the following: \n\n");
system("dir /B *.txt"); // list text files in directory

//cout << "\n\nPlease enter a file name: ";
printf("\n\nPlease enter a file name:");
//cin >> fileName; // Read in the file name
scanf("%s", fileName);

//cout << endl << endl ;
//inStream.open(fileName);

fp = fopen(fileName,"r");
if (!fp)
return;
int index = 0;
numberUsed = 0;
//while ( ! inStream.eof() && index < size )
while ((fscanf(fp, "%f", &a[index]) != EOF) && index < size)
{
//inStream >> a[index];
index++;
}
//inStream.close();
fclose(fp);
numberUsed = index;
}

void showArray(const float a[], int numberUsed)
{
//cout << "Array has the following values: \n\n";
printf("Array has the following values: \n\n");
//cout << "\tIndex\tValue\n";
printf("\tIndex\tValue\n");
for (int index = 0; index < numberUsed; index++)
{
//cout << "\t" << index << "\t"
//<< a[index] << endl;
printf("%d\t%f\t",index, a[index]);

}
}
aszxqw 2011-10-06
  • 打赏
  • 举报
回复
c语言读取的话我最经常用的方法是
先用 freopen("C:\\Users\\Desktop\\1.txt","r",stdin);
然后用scanf它会自动从那文件中读取。
hongwenjun 2011-10-06
  • 打赏
  • 举报
回复
既然用C++ 就直接用 fstream 吧,用起来比 C的简单点
Ol_lO 2011-10-06
  • 打赏
  • 举报
回复
fscanf,fread

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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