file IO in C

Bluga

Banned
Nov 28, 2000
4,315
0
0
i'm writing a program that accept two command line arguments (argv and argc). For each matrix, the first line contains a single integer n which is its dimension; each of the next n lines contains one row of the matrix. My question is: how can i extract the numbers in the input file so i can process it ?

For example, the input file:

3 <--------------------------------------------------- 3x3 MATRIX
3 5 7
1 3 4
0 1 8
5 <---------------------------------------------------5x5 MATRIX
3 45 -1 3 2
6 9 2 1 3
2 -6 4 2 3
2 3 0 0 1
1 1 9 5 7

all other numbers are elements of MATRIX. How can i seperate them? Thanks!

 

thornc

Golden Member
Nov 29, 2000
1,011
0
0
Have you tried reading the manual pages of your C compiler?
File i/o is handled among others by fopen, fread, fwrite, fclose and perhaps even fscanf!

There are others ways using streams, but those should be enough....
 

Bluga

Banned
Nov 28, 2000
4,315
0
0
yup, but all they have is reading the files until the EOF, how can i seperate my file since each line is used for different pruposes?
 

ermular

Member
Dec 24, 2001
143
0
0
you aren't describing what you need to do in any detail so that we can help you. argv and argc aren't command line arguments, argc is the number of command line arguments (including the executable name called to run your program) and argv[] is an array of character pointers that hold the command line arguments [0] to [argc-1].

in any case, we don't know what your arguments are or anything of that nature, so we can't do much to help you.

use a FILE pointer (FILE*) with fopen, fseek, fread (maybe fgets - it will read until it encounters a \n), fclose as the other guy said. the assignment isn't too difficult
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
So, let's clarify what exactly the program gets as input on the command line. Suppose your program's name is "test". Then if my input is stored in a file called data.txt, you would do this:

test data.txt

Correct? Then your "test" program has to read data.txt and process the data?
 

Bluga

Banned
Nov 28, 2000
4,315
0
0
Sorry for the confusion.

I need to implement the matrix determinant algorithm in a single file: ``det.c''. My program will accept two command line arguments ,the first one is the name of the input file and the second that of the output file. If the second argument is not given,i should create a default output file that has the name ``inputfilename.out''. For example, if my program is called: ``det myinput'', i should open ``myinput'' as my input file and ``myinput.out'' as the output file. If a file of the same name already exists, overwrite it.

For each matrix in the input file, the first line contains a single integer n which is its dimension; each of the next n lines contains one row of the matrix. My question is: how can i extract the numbers in the input file so i can process it ? If i use something like fscanf() how can i distinguish the dimension and the elements? Thank you.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
It's very simple, really. I whipped up something in 10 minutes: Demo Program

The above program will accept one input file and process it's contents. Check it out.
 

thornc

Golden Member
Nov 29, 2000
1,011
0
0
nice one singh,

I understood what you wanted Blunga, but I wasn't going to make to program like singh
did,, my idea was to make you think for yourself...

Afterall file parsing is basic programming stuff, if you can't figure that out for yourself, what
you'll you do when it get's to the hard part??
 

singh

Golden Member
Jul 5, 2001
1,449
0
0


<< nice one singh,

I understood what you wanted Blunga, but I wasn't going to make to program like singh
did,, my idea was to make you think for yourself...

Afterall file parsing is basic programming stuff, if you can't figure that out for yourself, what
you'll you do when it get's to the hard part??
>>



You are correct in that thinking, but in this particular case, I think the code explains itself - the explanation would probably be bigger than the code!
 

Bluga

Banned
Nov 28, 2000
4,315
0
0

Thanks alot singh. Just one question: When i ran the program it automatically jumped out, i didn't have option to enter the commands? Thanks again.
 

Bluga

Banned
Nov 28, 2000
4,315
0
0


<< nice one singh,

I understood what you wanted Blunga, but I wasn't going to make to program like singh
did,, my idea was to make you think for yourself...

Afterall file parsing is basic programming stuff, if you can't figure that out for yourself, what
you'll you do when it get's to the hard part??,
>>



actually this is just a small portion of my program, i already figured out the harder part of my program(matrix algorithm), i'm just not sure how to use the command lines.

:D
 

singh

Golden Member
Jul 5, 2001
1,449
0
0


<< Thanks alot singh. Just one question: When i ran the program it automatically jumped out, i didn't have option to enter the commands? Thanks again. >>



What do you mean? It's a command line program. Enter the input file name at the command line. Do you understand the code?
 

Bluga

Banned
Nov 28, 2000
4,315
0
0
yup i understand the code, but when i ran the program, the program automatically executed this line of code:

/* Do we have the correct input? */
if(argc != 2)
{
printf("Usage: <program name> FileName\n");
exit(0);
}

But the program never let me input anything. ?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Command Line arguments are used when you run the program from a command line. For example, if you executed the program from MS-DOS command prompt, or a Linux command line, you input the argument there. Do you understand?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Because he didn't design it to ask you for any input, it expects you to run it like 'mainx matrix_file' .
 

Bluga

Banned
Nov 28, 2000
4,315
0
0


<< Command Line arguments are used when you run the program from a command line. For example, if you executed the program from MS-DOS command prompt, or a Linux command line, you input the argument there. Do you understand? >>



oh that's right, stupid me. Thank you!
 

singh

Golden Member
Jul 5, 2001
1,449
0
0


<< oh that's right, stupid me. Thank you! >>



Np.. just don't do it again :)