• C

Data files/Array of Strings

I'm a beginner to C.
I have a data file with column headings that I want to read into an array of strings.  I've tried to set up a number of loops, but nothing works so far. What can I do?

Here's what I have so far--

     str = (char **)malloc(sizeof(char*),10);

     for(i=0; i<10; i++)
      {
     str[i] = (char *)malloc(sizeof(char),10);
     }
     
     for(i=0; i<10; i++)
     {
         temp = (char *)calloc(sizeof(char*), 10);
          fscanf(fp, "%s", temp);
          strcpy(str[i], temp);
     }          
     
     for(i=0; i<10; i++)
     {
          printf("\n %s \n", str[i]);
     }
     
               
     for(i=0; i<nrows; i++)
     {
          for(j=0; j<ncols; j++)
          {
          fscanf(fp, "%lf", &data[i][j]);
          }
     }
     
     printf("\nKnown? ");
     scanf("%s", &input1);
     printf("\nUnknown? ");
     scanf("%s", &input2);
     printf("\nKnown value? ");
     scanf("%lf", &x);
         
     for(i=0; i<=ncols; i++)
     {
          if(strcmp(input1, str[i])==0)
          {
               break;
          }
     }
     
     col_a = i;
     
     for(i=0; i<=ncols; i++)
     {
          if(strcmp(input2, str[i])==0)
          {
               break;
          }
     }
     
     col_b = i;


THANKS!
dkarpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

svatOplukCommented:
A few comments
char array[ARRAY_SIZE][STRING_SIZE];            --> static allocation for array of strings

for(i=0; i<ARRAY_SIZE; i++)
{
         buffer = malloc(STRING_SIZE);
         fscanf(fp, "%s", buffer);
         strcpy(array[i], buffer);
         printf("%s\n", str[i]);
}        

Try this to check if the file reading is ok.
0
GaryFxCommented:
Your first two mallocs should be calloc.

Your calloc to temp results in a memory leak.  This probably won't cause you any problems on a project this small, but you should move the allocation to before the loop.

Gary
0
gj62Commented:
If they want to use malloc in the first 2, just change it slightly to:

    str = (char **)malloc(sizeof(char*)*10);

    for(i=0; i<10; i++)
     {
    str[i] = (char *)malloc(sizeof(char)*10); /* you don't NEED sizeof(char) - that is guaranteed to be 1 */
    }
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

dkarpAuthor Commented:
Thanks for the answers so far.  I guess I'm also wondering if   there is any way to only allocate just enough memory for each string length rather than allocating enough for 10 characters every time?  
0
gj62Commented:
Sure can, here you go...  

char ** arrayOfStr;
char temp[11]; /*room for 10 characters, plus NULL*/

arrayOfStr = (char **)malloc(sizeof(char*)*10);
   
for(i=0; i<10; i++)
{
    fscanf(fp, "%s", temp);
    arrayOfStr[i] = malloc(strlen(temp)+1);
    strcpy(arrayOfStr[i], temp);
}          
   

With a little tweaking, you can even have a variable number in the arrayOfStr - you need to allocate one (just as you allocate 10), and then realloc() more each time you need more, e.g.

char ** arrayOfStr;
char temp[11]; /*room for 10 characters, plus NULL*/

arrayOfStr = (char **)malloc(sizeof(char*));
   
for(i=0; i<10; i++)
{
    fscanf(fp, "%s", temp);
    arrayOfStr = realloc(sizeof(char *)*i); /* allocate one more pointer in the array of strings*/
    arrayOfStr[i] = malloc(strlen(temp)+1); /* allocate string space, including room for NULL */
    strcpy(arrayOfStr[i], temp); /* copy string */
}          
   
0
gj62Commented:
Sorry - cut-and-paste bugs - here's a version that should actually work...

char ** arrayOfStr=NULL; /* you have to assign it a NULL value for the first realloc to work...*/
char temp[11]; /*room for 10 characters, plus NULL*/

 
for(i=0; i<10; i++)
{
   fscanf(fp, "%s", temp);
   arrayOfStr = (char **)realloc(arrayOfStr,sizeof(char *)*i); /* allocate one more pointer in the array of strings*/
   arrayOfStr[i] = (char *)malloc(strlen(temp)+1); /* allocate string space, including room for NULL */
   strcpy(arrayOfStr[i], temp); /* copy string */
}          

Now instead of a for loop, you can use while(!feof(fp)) or something similar - just remember to have a counter that you can use in realloc to allocate the correct number of pointers...
0
dkarpAuthor Commented:
I don't know what the deal is, but I can't get it to work.

Here's what I'm using:



char **str=NULL, temp[11];

str = (char **)malloc(sizeof(char*));
     
for(i=0; i<=ncols; i++) /*ncols = 10*/
{
     fscanf(fp, "%s", temp);
     str = (char **)realloc(str,sizeof(char *)*i);
     str[i] = (char *)malloc(strlen(temp)+1);
     strcpy(str[i], temp);
}




Thanks again
0
gj62Commented:
Two bugs, one mine, one yours... <grin>

remove the first malloc:  str = (char **)malloc(sizeof(char*));

You don't need, but it shouldn't be causing your bug.

Here's your bug - change this line:

str = (char **)realloc(str,sizeof(char *)*i);

to

str = (char **)realloc(str,sizeof(char *)*(i+1));

In the first instance, it was trying to allocate 0 pointers when i = 0 - that will never work...  my bad...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dkarpAuthor Commented:
gj62-

Thanks...it worked great!

-dkarp
0
gj62Commented:
Your welcome - good luck!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.