Solved

algorithm for my friend

Posted on 2004-04-20
4
213 Views
Last Modified: 2010-04-15
Hi experts,

int getdata(char *filename, ITEMS *items)
{
int i = 0;
char line [MAX_STR_LEN];
FILE *file_in;

if ( ( file_in = fopen (filename,"r")) == NULL) exit(EXIT_FAILURE);

 while ( fgets (line, MAX_LEN, file_in != NULL){

      sscanf (line,"%d %s", &item[i].number,item[i].string);
      i++;
 }
 return i;
}

Is this the best way to get data from a file ? How do I use malloc to replace MAX_STR_LEN so that I do not need to waste the space?
I would also like to do a timing algorithm to count the efficieny of my funtions, (eg sorting), how do I do that?
0
Comment
Question by:oldb
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10875312
Hi oldb,

> Is this the best way to get data from a file ?
Looks good ... using fscanf() would have been a bit more efficient ...

>How do I use malloc to replace MAX_STR_LEN so
> that I do not need to waste the space?
malloc will simply allocate the space in heap instead of stack ... you still need to allocate the space ... If you do not wish to use this memory, fscanf from the file directly into the array of ITEMS

for timing, use a profiling tool such as gprof or use clock () function

Sunnycoder
0
 
LVL 2

Expert Comment

by:anupvijay
ID: 10876138
As Sunny pointed out malloc doesn't save space but it gives the user the control to decide when you need the memory and
when you don't. So when you need it you allocate and when you don't need it you free it.
But in case of arrays as in ur case it is static memory on stack and will be invalid once out of scope. And you can't free it.

So depending on the case malloc allows you to use memory more efficiently by not holding a memory block when you
don't need it. If we wanna call it saving space then ....well....go ahead.

But in your case you do not know how many bytes you are gonna get from reading a line from the file.
So the char[] will do just good.

But if you need this array in some other function of your code then malloc(heap) might be a better idea.But it doesn't look like
you require that.

Hope you find the info useful.

Cheers.
0
 

Author Comment

by:oldb
ID: 10911730
I have actually written this, but this gives SEGMENTATION FAULT when I try to access items. (eg items->number)

typedef struct items ITEMS;

struct items{ int number;
                   char *string;
                   };

/* nel is the number of elements */
ITEMS *getdata(char *filename, int *nel){

int num;
char word[MAX_STR_LEN];
FILE *file_in;
ITEMS *items;

if ( (file_in = fopen(filename,'r')) == NULL)
               error;

while ( !feof(file_in)){

         fscanf(file_in ,"%d %s" , &num,word);
         (items+(*nel))->string = malloc(sizeof(char)*strlen(word) );
          items = realloc( items, sizeof(ITEMS)*(*nel+1) );

          (items+(*nel))->number = num;
          strcpy((items+(*nel))->string, word);
          *nel++;
           }

fclose(file_in);
return items;
}

When I run the programs, It gives a segmentation fault. I used GDB and think that it stops at the line where I access (items+(*nel))->string.

0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 80 total points
ID: 10918760
> (items+(*nel))->string = malloc(sizeof(char)*strlen(word) );
items here is an uninitialized pointer ... It can point to any memory location .... when you add *nel to it, you are adding *nel to some unknown qunatity ... When you dereference the result (->string), you are trying to dereference a random location which is not allocated to your program ... Thus your program crashes ...

To fix the issue, allocate memory to items before dereferencing it

items = (ITEMS *) malloc (sizeof(ITEM) * (*nel) ); ... perhaps you would like to initialize these elemets too before you derefernce them or you will get another seg fault for char * in the struct items (they will also have random values)
0

Featured Post

Industry Leaders: 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 and use conditional statements in the C programming language.

740 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