假设工程名称为Demo,以下代码演示如何使用命令行以及如何从文件中读取数据。
存储数据的文件名:demo.data,假设内容为:
8
5
15 1 3 41 3
1 2 4 3 32
2 1 4 32 2
8 1 6 86 5
9 1 6 3 25
6 6 31 2 6
5 5 2 23 7
9 9 0 3 76
/*
* this code demonstrate how to load data from a file and how to pass
* parameters to main function by command line or by directly setting them
* in the project
*/
#include <stdio.h>
#include <stdlib.h>
#define SUCCESS 1
#define FAILURE 0
#define MAXCOLUMN 20
#define MAXROW 20
class Test {
private:
int Data[MAXROW][MAXCOLUMN+1];
int NumRows; // Number of rows in array
int NumColumns; // Number of columns in array
public:
test();
int LoadData(char *fname); // Load data file to be displayed
};
int Test::LoadData(char *fname)
{
FILE *pfile;
int i, j, k;
if((pfile = fopen(fname, "r")) == NULL)
return FAILURE;
/*read data from file*/
fscanf(pfile, "%d", &NumRows); // Read # of Datas
fscanf(pfile, "%d", &NumColumns); // Read dimension of vector
for (i=0; i<NumRows; i++)
{
for (j=0; j<NumColumns; j++)
{
fscanf(pfile,"%d",&k); // consisting of all elements
Data[i][j]=k;
}
}
/*display data*/
for (i=0; i<NumRows; i++)
{
printf("Data[%d]=",i);
for(j=0; j<NumColumns; j++)
{
printf("%d ",Data[i][j]);
}
printf("\n");
}
return SUCCESS;
}
void main(int argc, char *argv[])
{
Test test;
if (argc<2)
{
printf("USAGE: DATA FILE\n");
exit(0);
}
if (test.LoadData(argv[1])==FAILURE )
{
printf("UNABLE TO READ DATA FILE:%s\n",argv[1]);
exit(0);
}
}
* this code demonstrate how to load data from a file and how to pass
* parameters to main function by command line or by directly setting them
* in the project
*/
#include <stdio.h>
#include <stdlib.h>
#define SUCCESS 1
#define FAILURE 0
#define MAXCOLUMN 20
#define MAXROW 20
class Test {
private:
int Data[MAXROW][MAXCOLUMN+1];
int NumRows; // Number of rows in array
int NumColumns; // Number of columns in array
public:
test();
int LoadData(char *fname); // Load data file to be displayed
};
int Test::LoadData(char *fname)
{
FILE *pfile;
int i, j, k;
if((pfile = fopen(fname, "r")) == NULL)
return FAILURE;
/*read data from file*/
fscanf(pfile, "%d", &NumRows); // Read # of Datas
fscanf(pfile, "%d", &NumColumns); // Read dimension of vector
for (i=0; i<NumRows; i++)
{
for (j=0; j<NumColumns; j++)
{
fscanf(pfile,"%d",&k); // consisting of all elements
Data[i][j]=k;
}
}
/*display data*/
for (i=0; i<NumRows; i++)
{
printf("Data[%d]=",i);
for(j=0; j<NumColumns; j++)
{
printf("%d ",Data[i][j]);
}
printf("\n");
}
return SUCCESS;
}
void main(int argc, char *argv[])
{
Test test;
if (argc<2)
{
printf("USAGE: DATA FILE\n");
exit(0);
}
if (test.LoadData(argv[1])==FAILURE )
{
printf("UNABLE TO READ DATA FILE:%s\n",argv[1]);
exit(0);
}
}
运行方法一:工程中运行
首先在 project -> Settings... -> Debug -> Program arguments 中输入入口参数 demo.data, 再编译运行
运行方法二:命令运行
将工程Demo所在目录的Debug目录下的Demo.exe和数据文件demo.data放在同一目录下,假设同时放在F盘根目录下。点击 开始 -> 运行,打开命令窗口,进入F盘根目录F:\>,输入Demo demo.data,回车。
输出结果:
Data[0]=15 1 3 41 3
Data[1]=1 2 4 3 32
Data[2]=2 1 4 32 2
Data[3]=8 1 6 86 5
Data[4]=9 1 6 3 25
Data[5]=6 6 31 2 6
Data[6]=5 5 2 23 7
Data[7]=9 9 0 3 76