I want to read a file character by character and add to an array.

while(!feof(inputfile)){
			chKey = getc(inputfile);
			for(int i = 0; i<=51; i++){
				chKey = enc[i];
			}
		}

Since there isn't like a add() function, how do you move the position by 1 and add something to an array in C.

Thanks

drjay

You need to increment the array index to point to the next element for each character read. Here's an example.

#include <stdio.h>
#define SIZE 1000

int main()
{
    int c, n, i;
    char a[SIZE];
    FILE *file;

    file = fopen ("filename.txt", "r");
    n = 0;
    while ((c = getc (file)) != EOF)
        a[n++] = c;
    fclose (file);

    for (i = 0; i < n; i++) putchar (a[i]);
}

You need to increment the array index to point to the next element for each character read. Here's an example.

#include <stdio.h>
#define SIZE 1000

int main()
{
    int c, n, i;
    char a[SIZE];
    FILE *file;

    file = fopen ("filename.txt", "r");
    n = 0;
    while ((c = getc (file)) != EOF)
        a[n++] = c;
    fclose (file);

    for (i = 0; i < n; i++) putchar (a[i]);
}

What's a terrible code! That's why we have troubles with buffer overflowed programs again and again ;)

for (n = 0; n < sizeof a && (c=getc(file)) != EOF; ++n)
    a[n] = c;

You need to increment the array index to point to the next element for each character read. Here's an example.

A problem that arise of giving what appears to be a full working example is that normally it gets taken like correct and proper working code, free of bugs and logic errors. Thus the common grateful responses of "it works great, thanks".
It is necessary if you are incline to do so, that at least you provide a minimum of good code practices, ensuring you are not spreading bad habits with your good intention. file = fopen ("filename.txt", "r"); Checking the return of fopen() is a must, affirming that the file can be read. int main() Don't leave anything implicit, make it explicit by returning a proper value, and passing the proper parameter[s].

while ((c = getc (file)) != EOF)
        a[n++] = c;

That while loop can read more than SIZE, if file is greater than 1000.
Initializing char a[SIZE]; to '\0' ensures if something fails and you display the array no garbage is shown or used.


Keep these kind of details in mind when you give examples, so you don't become a vector for bad practices.

This is what i ended up doing:

int i = 0;
while(!feof(inputfile)){
       chKey = getc(inputfile);
	enc[i] = chKey;
	i++;
}

It works the way I want it to work!

feof() is a poor choice to control a loop since feof() is true after the file EOF is read and not when EOF is reached. Meaning that while(!) will loop one more time before it knows it shouldn't.

What is your choice then?

You have been shown two possible choices already. The first one needs only a check boundary to not overflow the buffer. The second one is acceptable, other that I would use SIZE instead of sizeof(a), or I would set a variable to the sizeof(a) before entering in the loop. And I would terminated with a '\0' to make it a string after the loop.

This article has been dead for over six months. Start a new discussion instead.