×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Copying file contents to a char array

Copying file contents to a char array

Copying file contents to a char array

(OP)
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 */
}

RE: Copying file contents to a char array

#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, "USAGE display1 <filename>\n");
      exit (1);
   }
   
   f = fopen (argv[1], "r");
   if (f == NULL) {
      fprintf (stderr, "Unable to open file: %s\n", argv[1]);
      exit (2);
   }
   
  fillarray(f, words);
   
       if (fclose (f) != 0) {
      fprintf (stderr, "Unable to close the file!\n");
      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("Number of words counted: %d",word);
}

RE: Copying file contents to a char array

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, "USAGE display1 <filename>\n");
      exit (1);
   }
   
   f = fopen (argv[1], "r");
   if (f == NULL) {
      fprintf (stderr, "Unable to open file: %s\n", argv[1]);
      exit (2);
   }
   
  fillarray(f, words);
   
       if (fclose (f) != 0) {
      fprintf (stderr, "Unable to close the file!\n");
      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("Number of words counted: %d",word);
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close