Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Question on storing a couple of string into an array

Posted on 1998-03-23
11
Medium Priority
?
227 Views
Last Modified: 2010-04-10
With reference to the brief program segment below:

int index=0;
char *name[10];  // can hold up to a maximum of 10 strings
char str[50];
FILE *fp;

/* assume that file has been opened for reading */
while(fgets(str, 40, fp)) != NULL)
      strcpy(name[index++],str);

/* end of program segment */
Content of a file,
--------------------------------------------------
dog              0.123
cat              0.456
monkey           0.789
--------------------------------------------------

How can I store the just the name of the 3 animals in an
array called name? If possible, please include a program
segment or so to illustrate your point.

Is there anything wrong with my attempt of doing this?
Thanks for any help offered!
0
Comment
Question by:justinng
11 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 1183741
You'll probably get an error trying to write to an uninitialized pointer.
Try either
   char name[10][40];
or
   name[index++] = strdup(str);
0
 

Author Comment

by:justinng
ID: 1183742
erm.... thanx....and how about if I want to store only the
float value?

0
 
LVL 22

Expert Comment

by:nietod
ID: 1183743
The problem is that

char *name[10]  

is an array of 10 pointers to strings.  That is, it is an array that has 10 items.  Those items are pointers to strings, but those pointers have not yet been iniitalized to point to strings.  This your code write to what ever they point to.  That's bad.

You could go through array and initialize each pointer, like

for (int i = 0; i < 10; ++i)
   name[i] = new char[100];

This makes eack pointer point to a dynamically allocated string of 100 characters.  However, to clean up you will have to go though and deleted [] each string at the end.  

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 3

Expert Comment

by:q2guo
ID: 1183744
This should do it
------------------------------------

int index=0;
char name[10][50];  // can hold up to a maximum of 10 strings
float value[10];
FILE *fp;

/* assume that file has been opened for reading */
while(feof(fp) != NULL) {
      fscanf(fp, "%s", name[index])
      fscanf(fp, "%f" value);
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183745
ozo's idea is to not use an array of string pointer's but to use an array of strings.  The array he suggested stores 10 strings that are 40 characters long.  It actually has space for the strings, rather than pointers to the strings.  

Thee are advantages and dissadvantages to each.  An array of string pointers is good for cases where the strings will have different lengths because you can allocate memory for each string for just the length you need.  The array of strings is simpler and safer (less room for bugs) but you must declare each line long enough to hold the longest line and it therefore wastes space.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1183746
To store the float values you could create and array of floats and store them just like the strings.  You could create a structure that stores both a string and a float and create an array of these.  Then you could store both the name and the value in one array.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183747
I see q2quo has answered here and pointed out somthing I missed (ozo might of missed to)  You were running into the floats and needed to read them to get to the next string.  His algorithm will read and discard each float so that you can go on to the next string.
0
 

Author Comment

by:justinng
ID: 1183748
but how can i store just the float values into an array??
As nietod has pointed it out, my intention is to store the
name of the animal and the float values of it into 2 different
arrays one which is for the name of the animal and the other is
for the float values.
How should I go about it?

And also....how can I declare a long value of char???
I've tried the following,

long char *name[10];
char long *name[10];

but none of the above works in microsoft visual c++ 4.0....
it gives me an error saying its illegal.
Please correct me.
0
 

Author Comment

by:justinng
ID: 1183749
Oops....guess I missed out an important point in the content of the file,
-----------------
cats and dogs          0.123
mouse rats             0.456
elephant               0.789
-----------------

yeah....it should look like this.... anyway...I've managed to store the names into an array of strings (Thanks!) but it's just
the float values which is giving me a headache now :P

0
 
LVL 11

Expert Comment

by:alexo
ID: 1183750
take q2_guo's answer and replace
      fscanf(fp, "%f" value);
with
      fscanf(fp, "%f", value[index++]);

(I think his answer was valid)

There is no such animal as a "long value of char".  What were you trying to do?

0
 

Accepted Solution

by:
sllsgl earned 50 total points
ID: 1183751
char *name[10];  // can hold up to a maximum of 10 strings
 {WRONG, this without the * will only means that name can contain maximum 10 chars.}

To store 10 names, you can use CString Class, this is a very powerful class you will soon like it very much. You don't even have to worry if you have allocated enough memory space to store long, long name.

CString name[10];
--------------------------------------
if you still prefer char, use char name[10][50];   // this will allocate enough space for 10 names with maximum of 50 characters.  Make sure you do not exceed 20 chars in a single name.

To read string from the file, you can use
while (!feof(fp)) {
       fscanf(fp, "%s", name[i]);   // This will just read the characters until the white space.
       i++;
}

------------------------------
alternatively, read in the complete string first.
char str[81];

while (!feof(fp)) {
   fgets(str, 80, fp);   // will stop at end of line
   sprintf(name[i], "%s", str);  // read the name from str
   sprintf(number[i], " %f", str+strlen(name));
   i++;
}

To store just the float value, declare this first.
float number[10];

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.
Suggested Courses

580 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