Solved

some algorithm

Posted on 2004-04-20
8
224 Views
Last Modified: 2011-09-20
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:leeletir
8 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 10869274


I think that your best bet is to ask this question in the C++ or C forum.  C# would only use pointers in "unsafe" code, and that would be inefficient to do a simple task like opening a file.
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 10873055
Yes, your code looks like C++
0
 

Expert Comment

by:almurray
ID: 10877263
Hi leeletir,
Before using malloc - be sure you know why you want it. It is trickier to work with than static allocation. Static memory allocation, as you have it, is fine. But if you need dynamic allocation you can use malloc to allocate memory as you require it and then free the memory at the appropriate place. I have given examples of both C (malloc and free) and C++ (new and delete) memory allocation.

use 'malloc' and 'free' if you are working in a C environement
========================================

char *line;
line= (char*)malloc(MAX_STR_LEN);

***** IMPORTANT NOTE ******
Be sure to free your memory as follows when you are finished using it otherwise you will get a memory leak, which can cause your program to crash.

free(line);
------------------------------------------------------------------------

use 'new'and 'delete' if you are working in a C++ environement
========================================

char *line;
line= new char[MAX_STR_LEN];      

now use 'line' here until you are finsihed and only thne  can you free your memory
***** IMPORTANT NOTE ******
Be sure to delete your memory as follows when you are finished using it otherwise you will get a memory leak, which can cause your program to crash.

delete [] line;
------------------------------------------------------------------------
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!

 
LVL 14

Expert Comment

by:wayside
ID: 10878981
If your choice is between

char line[MAX_STR_LEN];

and

char *line = new char[MAX_STR_LEN];

it doesn't really matter, both use the same amount of memory. The difference is that one is on the stack and the other isn't.

If MAX_STR_LEN is small ( < 100,000, say) I wouldn't worry about wasting memory unless you have really restrictive requirements.

Could the string portion have spaces in it? If so, instead of

     sscanf (line,"%d %s", &item[i].number,item[i].string);
 
you might want to use something like

     sscanf (line,"%d %[^\r\n]", &item[i].number,item[i].string);
 
which will scan to the end of the line, rather than until the first space

> otherwise you will get a memory leak, which can cause your program to crash.

I don't see how a leak could cause a crash, unless you leak so much you run out of memory.
0
 

Expert Comment

by:almurray
ID: 10879252
The above example could cause a crash if the memory allocation is placed inside the while loop and never freed.

As memory allocation is a tricky area for newcommers to it, it is worthwhile pointing out the potential pitfalls. A worthwhile excercise for newcomers to dynamic memory allocation is to experiment a bit in a small program to get a feel for how it should and shouldn't be used. It may be even worthwhile to cause the program to crash to see it's effect, and then to try to rectify the problem.
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10897852
It looks like a C environment to me.
In C++ you better use streams ifstream or fstream.
In C++ it would be wiser to pass objects (like ITEMS) as reference and use stl containers like string dor the filename and map for the ITEMS.
0
 

Accepted Solution

by:
oldb earned 125 total points
ID: 10937573
/* nel is the number of elements */
ITEMS *getdata(char *filename){

int num,nel;
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;
}
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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 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