
Read file to array problem (Newbie for those wishing to avoid)
Quote:
> I am attempting to read from a text file called "team" which contains
I note "text file". It's important for the following...
Quote:
> individual integers on separate lines
So, the numbers have been converted to a text representation (probably
in decimal, without further notice).
Quote:
> into a static array and then printing to screen the contents of the
> array.
Why static? Is it required by the 'customer'?
Quote:
> Apart from a general lack of understanding I have two specific problems
> :-(.
> 1. The number of records reported as contained within the read file is
> incorrect
> 2. The values stored within the array do not correspond to those
> contained within the text file.
> The program is thus
> #####################################################################
> #include <stdio.h>
> FILE *read_file_ptr;
> int num_records ;
> main()
> {
> // declare static array called vec1
> int vec1[5];
This is nit a static array, but it's correct. Note that it's big enough for
5 int only.
Quote:
> if ((read_file_ptr = fopen("team", "r"))==NULL)
I prefer ...
read_file_ptr = fopen("team", "r");
if (read_file_ptr ==NULL)
... but it is only a matter of taste. The important thing is that you have
opened the file in text mode.
Quote:
> {
> printf("unable to open 'team' for reading\n");
> exit (1);
exit (EXIT_FAILURE); /* <stdlib.h> */
for maximum portability.
Quote:
> }
> // if number of elements in file team via file pointer is nil returns
> error
> fscanf(read_file_ptr, "%d", &num_records);
Ok, this can be done when entries are well formatted. The general way is to
read a line with fgets(), and then to convert the read line with the
appropriate functions (sscanf(), strtol() etc.).
Quote:
> printf("\nNumber of records in file team is %d\n\n", num_records);
Ah, you mean that the first line holds the number of records. Happy to
learn it! It could open to an interesting evolution to your code.
Quote:
> if (num_records ==0)
> {
> printf("file is empty!\n");
> exit(2);
Beware with non-portable exit values. Some can hang your computer. The
portable values are
0
EXIT_FAILURE
EXIT_SUCCESS
Quote:
Beware: you are using a C++ compiler or a C99 one. Be sure of what you are
doing.
Quote:
> for (i=0; i<6;i++)
This means 6 turns (0-5). Remember that your array has room for 5 int only
(0-4).
Quote:
> {
> vec1[i] = fgetc(read_file_ptr);
Wrong. You are reading the file byte by byte instead of reading it line by
line. At a first glance, you can reuse the previous code with fscanf(). But
you should really use fgets() and strtol() for example.
Quote:
> printf("The value of vec %d is %d\n", (i+1),vec1[i]);
> }
> }
> ##########################################################
> The file team contains
> 1
> 2
> 3
> 4
> 5
> 6
If I have well followed you, it should be:
6 (number of elements)
1 (value of [0])
2 (value of [1])
3 (value of [2])
4 (value of [3])
5 (value of [4])
Quote:
> and the output from the program is
> bala-483 $ fileread
> Number of records in file team is 1
Sure, the first line holds ' 1'
Quote:
> The value of vec 1 is 32
'space' in ASCII
Quote:
> The value of vec 2 is 50
'2' in ASCII
etc.
I let you doing the job. Feel free to post your code again for review or
debug.
--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/