DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Read from a file then placing strings into a 2d array (http://www.daniweb.com/forums/thread98567.html)

controlsi Nov 27th, 2007 4:45 am
Read from a file then placing strings into a 2d array
 

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

#define SIZE 5
#define MAX 10

void fillArray(char arr[][MAX]);
void printArray(char arr[][MAX]);
int fileOpen(void);

int main(void)
{
  char arr[SIZE][MAX];
  int success;
  fileOpen();
  if (success == 1)
  {
      fillArray(arr);
      printArray(arr);
  }
  else
        printf("File access failed");
  return 0;
}
int fileOpen(void)  //  ** this were i am having the most trouble
{
  char fileInput;
  char word[SIZE][MAX];
  FILE *input;
  int success;
  if ((input = fopen("infile", "r")) !=NULL)
  {
 
        while ((fileInput = fgetc(input)) != EOF)
            {
                        printf("\n file opened!");//remove
   
                printf("\n Its working!\n ");//emove
                strcpy(input, word);
 
                putchar(fileInput);
                fillArray(word);
            }
 
 
                printf("Failure in opening \"infile\" for writing.\n");
        fclose(input);
 }
  else
        {
          printf("\n Could not access \"infile\" for reading.\n\n");
          success = 0;
        }
  return success;
}

void fillArray(char word[][MAX])

  char arr[SIZE][MAX];  // i also need guidance on how to fill the array with the       
                                info from a the file "infile"
  int i, j;
  int names;
  names = 0;
  printf("FIllARRAY TIME!");
  for (i = 0; i < SIZE; i++)
        fscanf("%s", word[i]);
  for(j = 0; j < MAX; j++)
        if ( i < SIZE)
            if ( j < MAX)
          {
            printf("%s", i, word[i][MAX]);
            names = i;
            printf("Named pairs = %d", names);
            printf("\n\n");
          }
        else
          printf("problem with data presented!");
 
}
 
 
void printArray(char arr[][MAX])
{
  int i, j;
       
  for (i = 0; i < SIZE; i++)
  {
      for (j = 0; j < MAX; j++)
        printf("%s", arr[i][j]);
      printf("\n");
  }
           
}

any in sight would be helpful. im really under the gun and have been trying to get this to work for a couple of days.

thanks in advance
David

WaltP Nov 27th, 2007 5:58 am
Re: Read from a file then placing strings into a 2d array
 
Quote:

Originally Posted by controlsi (Post 479139)
any in sight would be helpful. im really under the gun and have been trying to get this to work for a couple of days.

thanks in advance
David

With what? You didn't mention anything was wrong. Did you read the post titled
Read Me: Read This Before Posting?

controlsi Nov 27th, 2007 10:12 am
Re: Read from a file then placing strings into a 2d array
 
sorry about that and thank you for formatting my code. I will make sure to do that from now on and i took your advice and read the post mentioned above.

Well, my problem is that the code above is suppose to read strings from a file and then take those strings and place them into a 2d array.

The problem that i am having is reading the actual strings from the text file and placing them into the array. It only seems to be printing the first letter in the file then nothing else.

input:
infile(which is the txt file i am using) contains:
lastname, firstname (about 5 names)
and the names are not sorted

output:
the names are printed here exactly as they are shown in the text file "infile"
ex.
lastname, firstname
doe, john
francis, chris

any help would be very appreciated

WaltP Nov 27th, 2007 4:29 pm
Re: Read from a file then placing strings into a 2d array
 
Try commenting your code. That would tell you quite a lot, if you comment exactly.

Based on your code:
   if ((input = fopen("infile", "r")) !=NULL)    // open the file
  {
 
        while ((fileInput = fgetc(input)) != EOF)    // read a single character
            {
                strcpy(input, word);    // copy WORD into INPUT
 
                putchar(fileInput);    // Output the character read
                fillArray(word);
            }
You never did anything with the single character you read. And you never read any more than that.
Isn't INPUT a file designator? Does
strcpy()
work on such an item? Look it up. I know your compiler complained about that.


All times are GMT -4. The time now is 4:03 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC