reading file characters into an int array
Hey All,
I'm going through C Primer Plus (4th Edition). Chapter 13 is about File Input/Output. The 12th exercise is totally stumping me, and here's why...
It wants me to open an already existing text file (that I created as per book instruction), and then copy the characters into an int array. Then, I'm supposed to take that array, and create another array (this time type char), with which I can trade out characters during transfer, thus making a crude digital picture. The pre-existing text file has a 20x30 array of single digits separated by spaces (making the lines 60 characters long).
Example line from text file (first line out of 20):
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 2 0 0 0 0 0 0 0 0 0 0 0
The problem is I've already figured out how to open the file, alter the data and print it to either the screen or a new file... but only if I skip the extra step of copying the original text file characters to the int array.
I feel like it must be important for some reason for me to be able to copy text characters into an int array from an open file stream, otherwise the book wouldn't ask me to do it.
Here's one of my failed solutions so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#define LINES 20
#define CHARS 30
int main(void)
{
int ch; // doesn't work with 'char ch' either
int a, b;
FILE * fpsrc;
int numbers[LINES][CHARS]; // array to hold all file characters as integers
if ((fpsrc = fopen("picture1.txt", "r")) == NULL) // open to read-only
{
printf("Sorry, couldn't find or open \"%s\".\n", "picture1.txt");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(fpsrc)) != EOF) // doesn't work with getc() either
{
if (ch == 32)
continue;
else if (ch == 48)
{
numbers[a][b] = 0;
b++;
continue;
}
else if (ch == 49)
{
numbers[a][b] = 1;
b++;
continue;
}
else if (ch == 50)
{
numbers[a][b] = 2;
b++;
continue;
}
else if (ch == 51)
{
numbers[a][b] = 3;
b++;
continue;
}
else if (ch == 52)
{
numbers[a][b] = 4;
b++;
continue;
}
else if (ch == 53)
{
numbers[a][b] = 5;
b++;
continue;
}
else if (ch == 54)
{
numbers[a][b] = 6;
b++;
continue;
}
else if (ch == 55)
{
numbers[a][b] = 7;
b++;
continue;
}
else if (ch == 56)
{
numbers[a][b] = 8;
b++;
continue;
}
else if (ch == 57)
{
numbers[a][b] = 9;
b++;
continue;
}
else if (ch == '\n')
{
a++;
continue;
}
}
if (fclose(fpsrc) != 0)
fprintf(stderr, "Error closing file.\n");
return 0;
}
This doesn't include the rest of the stuff, but like I said, moving the data onto the screen or into a new file has been the easy part.
I've tried other while loops, using fscanf() and others using fgets(). No dice.
I'm not looking for actual code, just some advice as to why I'm failing so miserably in simply transferring data from a stream to an int array.
Thanks!
more streamlined code for anyone still interested
If anyone is still interested in this little exercise...
In the same way that using fscanf() allowed me to approach the problem from a different direction, thus cutting down the necessary code significantly, I realized that my approach to changing the characters had bloated code as well.
Here's a simpler way to transfer the "altered" integers in the (int) 'numbers' array to the char 'array', using nested 'for' loops instead of the 'while' statement (with all that extra b++; continue; crap) I was using in the initial example in this post:
Code:
for (a = 0; a < LINES; a++) // These nested 'for' loops "alter" the digits in the int array
{ // called 'numbers[][]' as they are transferred to the char
for (b = 0; b < CHARS; b++) // array called 'array[][]'
{
num = numbers[a][b];
if (num == 0)
array[a][b] = ' ';
if (num == 1)
array[a][b] = '-';
if (num == 2)
array[a][b] = '*';
if (num == 3)
array[a][b] = '=';
if (num == 4)
array[a][b] = 'x';
if (num == 5)
array[a][b] = 'Q';
if (num == 6)
array[a][b] = '$';
if (num == 7)
array[a][b] = '&';
if (num == 8)
array[a][b] = '%';
if (num == 9)
array[a][b] = '#';
}
}
I realize I should be using a 'switch' statement instead of a bunch of 'if' statements, but I figured I wouldn't really save much typing anyway.
sf