Read file to array problem (Newbie for those wishing to avoid) 
Author Message
 Read file to array problem (Newbie for those wishing to avoid)

I am attempting to read from a text file called "team" which contains
individual integers on  separate lines
into a static array  and then printing to screen the contents of the array.

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];

 if ((read_file_ptr = fopen("team", "r"))==NULL)

      {
      printf("unable to open 'team' for reading\n");
      exit (1);
      }

// if number of elements in file team via file pointer is nil returns error

 fscanf(read_file_ptr, "%d", &num_records);

 printf("\nNumber of records in file team is %d\n\n", num_records);

 if (num_records ==0)

      {
      printf("file is empty!\n");
      exit(2);
      }

 int i;

 for (i=0; i<6;i++)
      {
      vec1[i] = fgetc(read_file_ptr);
      printf("The value of vec %d is %d\n", (i+1),vec1[i]);
      }

 }

##########################################################

The file team contains

1
2
3
4
5
6

and the output from the program is

bala-483 $ fileread

Number of records in file team is 1

The value of vec 1 is 32
The value of vec 2 is 50
The value of vec 3 is 32
The value of vec 4 is 51
The value of vec 5 is 32
The value of vec 6 is 52
bala-484 $

I am baffled (and obviously missing the point by a mile). Could anyone
enlighten me - if only to point me in the direction of an appropriate C
function if I am using the wrong one.

Cheers

Alan

--
To reply change red to blue.



Fri, 04 Jun 2004 00:40:41 GMT  
 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:
>       }

>  int i;

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.

- Show quoted text -

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/



Fri, 04 Jun 2004 01:20:50 GMT  
 Read file to array problem (Newbie for those wishing to avoid)

Thanks :-)

 I shall digest and return.

Cheers

Alan



Fri, 04 Jun 2004 01:28:58 GMT  
 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
> individual integers on  separate lines into a static array  and then
> printing to screen the contents of the array.

> 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>

  #include <stdlib.h>
for exit()
Quote:

> FILE *read_file_ptr;
> int num_records ;

There is absolutely no reason for these to be global, except that you
assume later that num_records is initialized to zero, which you could do
manually inside main.
Quote:

> main()

>  {
> // declare static array called vec1

Ok, for starters, this is useless. Comments should explain the ideas
behind code, not simply the semantics of the code, which are fairly
obvious. For example:
x=((x+50)/100)*100; /*set x equal to x plus 50 divided by 100
                      then multiplied by 100*/

x=((x+50)/100)*100; /*round x to the nearest hundred*/

Which is more helpful?
Secondly, it's wrong. vec1 isn't static, it's automatic.

Quote:
>  int vec1[5];

I'd say this is a good time to use malloc, but at the very least make
vec1 big enough to hold all the elements in your file.
Quote:

>  if ((read_file_ptr = fopen("team", "r"))==NULL)

>       {
>       printf("unable to open 'team' for reading\n");

This message should probably go to stderr.
Quote:
>       exit (1);

EXIT_FAILURE may be a better choice, since it indicates failure, whereas
1 has an implimentation defined meaning.
Quote:
>       }

> // if number of elements in file team via file pointer is nil returns
> error

>  fscanf(read_file_ptr, "%d", &num_records);

You should test the return value of fscanf.
Quote:

>  printf("\nNumber of records in file team is %d\n\n", num_records);

This assumes that the first integer in your file is the number of
records. It isn't. Either add it, so your file looks like:
6
1
2
3
4
5
6
or read the file until you reach the end, counting the number of elements.
Quote:

>  if (num_records ==0)

>       {
>       printf("file is empty!\n");

A file which contains the number 0 will cause this message as well.
Quote:
>       exit(2);
>       }

>  int i;

Allowed in C99, but not C90. Older compilers might balk.
Quote:

>  for (i=0; i<6;i++)

you've walked right off the end of the array, not to mention totally
ignoring the num_records field that you read earlier. Try
   for(i=0;;i<sizeof(vec1)&&i<num_records;i++)
Quote:
>       {
>       vec1[i] = fgetc(read_file_ptr);

fgetc will get the character code, it will not convert it to an int, and
it won't skip white space. You want to use fscanf or fgets and strtol,
and check for errors.
Quote:
>       printf("The value of vec %d is %d\n", (i+1),vec1[i]); }

>  }

<rest snipped>


Fri, 04 Jun 2004 02:13:07 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Question from a Newbie :: Reading matrices from a file to arrays

2. Newbie problem reading text files

3. Newbie on VC++5 : Problem reading files and putting them in CString

4. Newbie Array Problem - how to strncpy from an array of char

5. Newbie to VC++ wishing to port from Unix

6. File reading problem - reading a text colormap.

7. newbie reading files

8. Newbie needs help reading binary files

9. Newbie - Read Text Files ques!

10. Newbie reading in data from ASCII file to structure

11. Newbie: Reading in formated data from file!

12. NEWBIE looking to read text file into dynamically-linked queue

 

 
Powered by phpBB® Forum Software