69,371
社区成员
发帖
与我相关
我的任务
分享
//#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]);
}
}