Selectively reading from file into an array

Oct 9, 2013 at 12:32am
Hello.
Would anyone please give me a hint how to solve my problem?
I'm reading a file into a char array.
On the first line in the text file there are two numbers which
specify the size of the array, ie "20 20".
Starting on the next line is the array of characters
(characters are arranged in a 20x20 grid with white spaces
between them)

First, I'm using fscanf in a 'for' loop to read the numbers and make a note of them. I'm using them to create a char array of that size for holding the letters.
Next I read in the grid of letters, like so

1
2
3
4
5
6
7
8
9
10
11
12
  i=0;
  j=0;
  while((ch=fgetc(fp))!=EOF){
    if(isalpha(ch)){
       array[i][j]=ch;
       j++;
       if(j==width of the array){
          i++;
          j=0;
       }
     }
   }


As you can see, this code reads the whole file from the beginning
and ignores those two numbers that are on the first line of the file.
All is good but... I also need to make sure that there aren't any
stray numbers that might have found it's way into the grid of letters.
And if there is, I should warn the user and abort.
The way my code is right now, any numbers will be simply ignored.
But I need to be able to differentiate between the two numbers
at the beginning of the file and those that might happen to be in the
grid of letters(a typo,for example).
Any thoughts?

Thank you.
Oct 9, 2013 at 12:59am
closed account (D80DSL3A)
Maybe something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while((ch=fgetc(fp))!=EOF){
    if(isalpha(ch)){
       array[i][j]=ch;
       j++;
       if(j==width of the array){
          i++;
          j=0;
       }
     }
     else if(isdigit(ch))
     {
         cout << "ERROR! A number has been found amongst the characters!\n";
         cout << "Please correct the file data and try again.\n";
         exit(EXIT_FAILURE);// terminate program
      }
   }
Oct 9, 2013 at 1:14am
But, won't this code exit than, as soon as it encounters those two numbers
at the beginning of the file?
I need to ignore those and exit only if there are numbers int the grid of letters.
Oct 9, 2013 at 1:20am
closed account (D80DSL3A)
I thought those 2 numbers had been read before the code shown.
From your post:
First, I'm using fscanf in a 'for' loop to read the numbers and make a note of them. I'm using them to create a char array of that size for holding the letters.
Next I read in the grid of letters, like so
and your code follows...
Oct 9, 2013 at 1:30am
Yea, but when the while loop starts doesn't it start reading file from the beginning again? Or is it that, after I used fscanf to read in those two numbers,
the internal pointer remains where fscanf left off? And fgetc will pick up from there?
It would be nice :)
Last edited on Oct 9, 2013 at 1:32am
Oct 9, 2013 at 2:37am
closed account (D80DSL3A)
I think it does work that way, though I haven't used those c language based methods in many years.
You could look up the functions to find out how they work.
Or, you could try the code (run the program) and see what happens.
You can experiment with code. It won't harm your computer. I learn many things this way.
Oct 9, 2013 at 3:10am
:-)

Thank you.
Topic archived. No new replies allowed.