Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

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!
0
dkarp
Asked:
dkarp
1 Solution
 
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
Independent Software Vendors: 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!

 
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
 
dkarpAuthor Commented:
gj62-

Thanks...it worked great!

-dkarp
0
 
gj62Commented:
Your welcome - good luck!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now