Solved

some algorithm

Posted on 2004-04-20
8
225 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
[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
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
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!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

687 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