Solved

algorithm for my friend

Posted on 2004-04-20
4
215 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
[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
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

617 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