Hi all,

I'm working in the C language.

I've been stuck for 2 weeks trying to figure out how in the world to populate arrays after reading in an input FILE. the input file has numbers and words, I need to write a loop that grabs the integers and stores them in an int array, and also store the characters into the char array. Please Help I'm so desparate. My teacher is no help because he just gets angry at you for not knowing. kinda dumb since im just learning how to do it all. (GEEZ.)


here's what i got....

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

void read();

int main()
{
   read();
   return 0;  
}

void read()
{
   char w[1000]; 
   int n[1000];
   int i;
   FILE *f;
   f = fopen("input.txt","r");
      
   while(i < 1000)
   {      
      if (fscanf(f, "%c%d", &w[i], &n[i])!= EOF)
      {
         printf("%c%d", w[i], n[i]);         
      }      
      else
      {
         break;
      }                
   }          
   fclose(f);   
}

this doesn't work, but I'm stuck.

Thanks for the help

Hello,

First thing: you never initialize i. Delare i as int i= 0;

input file has numbers and words

if (fscanf(f, "%c%d", &w[i], &n[i])!= EOF)

You're scanning for char's not words. You need a 2 dimensional array of chars if you want to store all the words.

Could you give an example of what the inputfile looks like?

Edited 6 Years Ago by Nick Evan: n/a

Hello,

First thing: you never initialize i. Delare i as int i= 0;

if (fscanf(f, "%c%d", &w[i], &n[i])!= EOF)

You're scanning for char's not words. You need a 2 dimensional array of chars if you want to store all the words.

Could you give an example of what the inputfile looks like?

Oh yes I was editing the file i forgot to add that "int i" intializes at zero. thanks.

the input file looks like this....

plants
2
trees
4
dogs
35
bark
15
bear
1
tiger
40

Edited 5 Years Ago by Nick Evan: n/a

You can't use fscanf(f, "%c%d", &w[i], &n[i]) try fgets(), it's reads a file line by line an returns 0 when EOF. Then every even line use a function like atoi() to convert the number to int and your done.

Edited 6 Years Ago by Nick Evan: n/a

You can't use fscanf(f, "%c%d", &w[i], &n[i]) try fgets(), it's reads a file line by line an returns 0 when EOF. Then every even line use a function like atoi() to convert the number to int and your done.

I was not familiar with atoi().

This is my first class ever for computer science in college. and this is a lab exercise. When I went for help and asked my teacher he told me that I should only have to use fscanf in order to get this done so i've been banging my head against the wall to figure this out using fscanf. I did get fgets to work but only to printf the file on the screen it didn't populate the array correctly just kept writing over the array while in a loop. so i was unable to grab from anywhere in the array only the last bit of the input file that the loop read in. I did that with a program two weeks ago, but have been stuck on the new concepts since then.

do you think that atoi() is the way to go? I don't remember that at all from lecture.

thanks for the help

Edited 5 Years Ago by Nick Evan: n/a

allright, if you have to use fscanf, you could use it something like this:

char acString
int iNr = 0;
FILE *f;
f = fopen[B]([/B]"input.txt","r"[B])[/B];
 
while(1) 
{
if (fscanf(f, "%s", acString)== EOF) break;
if (fscanf(f, "%d", iNr)== EOF) break;
printf("tada: \n %d %s", iNr, acString);
}

I didn't compile/test it..

Edited 5 Years Ago by Nick Evan: n/a

I got it to work sort of...

#include<stdlib.h>
#include<stdio.h>
void read();
 
int main()
{
   read();
   return 0;  
}
 
void read()
{
   char w[1000]; 
   int n[1000];
   int i=0;
   FILE *f;
   f = fopen("input.txt","r");
      
   while(i < 1000)
   {      
      i++;
      if (fscanf(f, "%s%d", w, &n[i])!= EOF)
      {
         printf("%s%d", w, n[i]);         
      }      
      else
      {
         break;
      }                      
   }             
   fclose(f);   
}

I think the problem with your code and mine is it just overlaps the character array in the loop. (well mine for sure does) I just don't know what to do from there.?????

thanks a lot you have been a big help.

No problem!

one thing: int n[1000]; you don't need an array if you're going to print them right away. One int is enough.

P.s. Could you mark this thread as 'solved'?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

No problem!

one thing: int n[1000]; you don't need an array if you're going to print them right away. One int is enough.

P.s. Could you mark this thread as 'solved'?

No this problem is not solved.

because I need to store all of the information into the data structure so that I can mess with it and update info. The initial input is just to get my array's populated and then from there the program will ask the user to enter info that would update the arrays and NOT the FILE. This is just the very beginning to the whole program.

These are the exact Objectives for this program....
- To experience and understand records/structures in C.
- To learn about and create arrays of records.
- To practice using FILE I/O.
- To practice developing functions to sort array data.

Please help. sorry for the late response had to go to bed.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

For practical purposes if w is declared as

char w[1000];

then w can only hold 1 string. If you want w to be an array of strings, then you want this:

char w[x][y];

or one of these:

char ** w;
char * w[x];

where x and y are positive constant integers. Based on the 2PM post, you probably want something like this however:

typedef struct Record
{
   char * w[80];
   int num;
};

Record records[1000];

In this scenario each Record has two fields, a string and an int and records is an array of Record objects. To read a Record from the file into records you could do something like this:

//declare a FILE pointer, fp, and associate it with the desired file to be read here
int i = 0;
while(i < 1000)
{
   if(fscanf(fp, "%s", records[i].w) != EOF)
   {
     fscanf(fp, "%d", &records[i].num);
   }
   ++i;
}

The Record objects in records can be sorted based on either field. The sorting could be done either at time of addtion to the array, or after all elements of the array have been entered, which is the usual way. There are probably 10 different sorting procedures that could be used depending on your knowledge and available tools. Once you've sorted parallel arrays vs an array of struct you'll appreciate the value of structs.

Beware, I don't routinely use C style I/O so whether you need the address of operator for num but not w as in my example, or whether both or neither need the address operator, or whether both need the address operator, you'll have to work out yourself, but I think this is the correct way.

For practical purposes if w is declared as

char w[1000];

then w can only hold 1 string. If you want w to be an array of strings, then you want this:

char w[x][y];

or one of these:

char ** w;
char * w[x];

where x and y are positive constant integers. Based on the 2PM post, you probably want something like this however:

typedef struct Record
{
   char * w[80];
   int num;
};

Record records[1000];

In this scenario each Record has two fields, a string and an int and records is an array of Record objects. To read a Record from the file into records you could do something like this:

//declare a FILE pointer, fp, and associate it with the desired file to be read here
int i = 0;
while(i < 1000)
{
   if(fscanf(fp, "%s", records[i].w) != EOF)
   {
     fscanf(fp, "%d", &records[i].num);
   }
   ++i;
}

The Record objects in records can be sorted based on either field. The sorting could be done either at time of addtion to the array, or after all elements of the array have been entered, which is the usual way. There are probably 10 different sorting procedures that could be used depending on your knowledge and available tools. Once you've sorted parallel arrays vs an array of struct you'll appreciate the value of structs.

Beware, I don't routinely use C style I/O so whether you need the address of operator for num but not w as in my example, or whether both or neither need the address operator, or whether both need the address operator, you'll have to work out yourself, but I think this is the correct way.

Awesome. I think you just hit the nail on the head for what I was looking for. for this class I first had to get familiar with the parallel array (which I never got) and now work with structs. So I will make a code for each to get familiar with them both of them. Great help. Thanks. after I am finished I will label topic as solved, but not just yet.
If anyone is interested here is the link to the lab exercise. and another which is almost identical and may be easier I don't know.

if anyone else has suggestions feel free to post. This topic is so hard to find on the web. I spent hours looking.

P.S. that is wierd that you put

char * w[80]

that is exactly what this lab that i'm doing required for the longest length the character could be. I'd sware you were either my teacher or a student in this class.

This question has already been answered. Start a new discussion instead.