Link to home
Start Free TrialLog in
Avatar of dkarp
dkarp

asked on

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!
Avatar of svatOpluk
svatOpluk

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.
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
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 */
    }
Avatar of dkarp

ASKER

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?  
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 */
}          
   
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...
Avatar of dkarp

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of gj62
gj62

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkarp

ASKER

gj62-

Thanks...it worked great!

-dkarp
Your welcome - good luck!