?
Solved

Data files/Array of Strings

Posted on 2003-03-27
10
Medium Priority
?
180 Views
Last Modified: 2010-04-15
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!
0
Comment
Question by:dkarp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Expert Comment

by:svatOpluk
ID: 8220945
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
 
LVL 6

Expert Comment

by:GaryFx
ID: 8221109
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
 
LVL 6

Expert Comment

by:gj62
ID: 8221426
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dkarp
ID: 8221784
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
 
LVL 6

Expert Comment

by:gj62
ID: 8222007
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
 
LVL 6

Expert Comment

by:gj62
ID: 8222019
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
 

Author Comment

by:dkarp
ID: 8222261
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
 
LVL 6

Accepted Solution

by:
gj62 earned 240 total points
ID: 8222323
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
 

Author Comment

by:dkarp
ID: 8222402
gj62-

Thanks...it worked great!

-dkarp
0
 
LVL 6

Expert Comment

by:gj62
ID: 8222923
Your welcome - good luck!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question