I have a txt file which has the following input.
13
.34
67
45
12
4
85
56
42
44
2
8
3
5
2
the first number is the size, second is a multiplier, last one is used for search. I am wondering if the code below looks correct. The input text file name is "input.txt" or if there is a better way of collected the data and storing them for later use?


int main(int argc, char * argv[]){
int p;
double alpha;
int A[12];
int s;

if (argc<2){

printf("Error: include the name of the input file as an argument\n");

return 0;
}

printf("Attempting to open file %s\n",argv[1]);

FILE * F = fopen(argv[1],"input.txt");

printf("The file is now open for reading\n");

printf("Attempting to read \n");

fscanf(F,"%d, %lf, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d",&p &alpha, A[0],A[1],A[2],A[3],A[4],A[5],A[6],A[7],A[8],A[9],A[10],A[11], &s ); // this line will crash if the file doesn't exist

printf("Read number %d from the file\n",number);

fclose(F);

printf("The file is now closed\n");

return 0;
}

Recommended Answers

All 4 Replies

I would say that this is supposed to be a C question and even that it is poorly written. Whats your question ?

And use code tags -- see that link that says [code] ? That wasn't put there just for decorations.

I would say that this is supposed to be a C question and even that it is poorly written. Whats your question ?

Thanks for the critiism, but really I was looking for more help in how to make the code more efficient in C++. Any ideas?

Well, Instead of criticizing our so called criticizm. Why dont you use a for loop to take in input .. And While you are at it. try to use cout and cin as printf and scanf are Considered as 'C' though they work in c++,

Apart from that, Instead of using a FILE* try using fstream

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.