I'm having some trouble copying the contents of a file to a char array, any help anyone could provide would be apprecaiated:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
char words [10000][30];
FILE *f; /* disk input file */
char ch; /* keyboard input character */
int word;
int count;
count = 1;
word = 1;
if (argc != 2) {
fprintf (stderr, "USAGE display1 <filename>\n"
;
exit (1); /* error termination */
}
f = fopen (argv[1], "r"
;
if (f == NULL) {
fprintf (stderr, "Unable to open file: %s\n", argv[1]);
exit (2);
}
for (;
{
ch = fgetc (f); /* from disk... */
if(ch == ' ' || ch == '\n'){
//strcpy(words[word],ch);
words[word] = ch;
word++;
}
if (feof (f))
break;
}
printf("Number of words read: %d\n",word);
//for (count = word; count != 0; count--){
printf("word: %s\n", words[1]);
//}
if (fclose (f) != 0) {
fprintf (stderr, "Unable to close the file!\n"
;
exit (3);
}
exit (0); /* normal termination */
}
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
char words [10000][30];
FILE *f; /* disk input file */
char ch; /* keyboard input character */
int word;
int count;
count = 1;
word = 1;
if (argc != 2) {
fprintf (stderr, "USAGE display1 <filename>\n"
exit (1); /* error termination */
}
f = fopen (argv[1], "r"
if (f == NULL) {
fprintf (stderr, "Unable to open file: %s\n", argv[1]);
exit (2);
}
for (;
ch = fgetc (f); /* from disk... */
if(ch == ' ' || ch == '\n'){
//strcpy(words[word],ch);
words[word] = ch;
word++;
}
if (feof (f))
break;
}
printf("Number of words read: %d\n",word);
//for (count = word; count != 0; count--){
printf("word: %s\n", words[1]);
//}
if (fclose (f) != 0) {
fprintf (stderr, "Unable to close the file!\n"
exit (3);
}
exit (0); /* normal termination */
}