Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying file contents to a char array

Status
Not open for further replies.

bob323

MIS
Jan 3, 2001
5
0
0
US
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, &quot;USAGE display1 <filename>\n&quot;);
exit (1); /* error termination */
}

f = fopen (argv[1], &quot;r&quot;);
if (f == NULL) {
fprintf (stderr, &quot;Unable to open file: %s\n&quot;, 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(&quot;Number of words read: %d\n&quot;,word);

//for (count = word; count != 0; count--){
printf(&quot;word: %s\n&quot;, words[1]);
//}

if (fclose (f) != 0) {
fprintf (stderr, &quot;Unable to close the file!\n&quot;);
exit (3);
}
exit (0); /* normal termination */
}
 
#include <stdio.h>
#include <stdlib.h>

#define IN 1
#define OUT 0

void fillarray(FILE *f, char words [][]);

int main (int argc, char *argv[])
{
FILE *f;
int count;
/*static spaced used because stack space is more limited*/
static char words [10000][30];

count = 1;

if (argc != 2) {
fprintf (stderr, &quot;USAGE display1 <filename>\n&quot;);
exit (1);
}

f = fopen (argv[1], &quot;r&quot;);
if (f == NULL) {
fprintf (stderr, &quot;Unable to open file: %s\n&quot;, argv[1]);
exit (2);
}

fillarray(f, words);

if (fclose (f) != 0) {
fprintf (stderr, &quot;Unable to close the file!\n&quot;);
exit (3);
}

return 0;
}

void fillarray(FILE *f, char words [][])
{
int word;
char currentchar;
int state;
int space;
word = 0;
space = 0;
state=OUT;

while ((currentchar = fgetc(f)) != EOF){

if (currentchar == ' ' || currentchar == '\n' || currentchar == '\t')
state = OUT;

else if (state == OUT){
state = IN;
++word;
}

if (state == IN){
words[word][space] = currentchar;
space++;
}
}
printf(&quot;Number of words counted: %d&quot;,word);
}
 
This doesn't work but its closer!
sorry!

#include <stdio.h>
#include <stdlib.h>

#define IN 1
#define OUT 0

void fillarray(FILE *f, char words [][]);

int main (int argc, char *argv[])
{
FILE *f;
int count;
/*static spaced used because stack space is more limited*/
static char words [10000][30];

count = 1;

if (argc != 2) {
fprintf (stderr, &quot;USAGE display1 <filename>\n&quot;);
exit (1);
}

f = fopen (argv[1], &quot;r&quot;);
if (f == NULL) {
fprintf (stderr, &quot;Unable to open file: %s\n&quot;, argv[1]);
exit (2);
}

fillarray(f, words);

if (fclose (f) != 0) {
fprintf (stderr, &quot;Unable to close the file!\n&quot;);
exit (3);
}

return 0;
}

void fillarray(FILE *f, char words [][])
{
int word;
char currentchar;
int state;
int space;
word = 0;
space = 0;
state=OUT;

while ((currentchar = fgetc(f)) != EOF){

if (currentchar == ' ' || currentchar == '\n' || currentchar == '\t')
state = OUT;

else if (state == OUT){
state = IN;
++word;
}

if (state == IN){
words[word][space] = currentchar;
space++;
}
}
printf(&quot;Number of words counted: %d&quot;,word);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top