Connecting Tech Pros Worldwide Forums | Help | Site Map

array of string

Newbie
 
Join Date: Jul 2006
Posts: 1
#1: Jul 7 '06
Hi,

How to create an array of string from an input file? I was using the following cods and got error.

Ex:
Input file: suit.txt has 4 lines
hearts
diamonds
clubs
spades

code:
char *suitArr[4];
...
ifstream infile("suit.txt", ios::in);
...
for (int x=0; x < 4; x++)
{
infile.getline(inlineBuf,20);
char *suiteArr[x] = new char [strlen(inlineBuf) +1);
assert( suiteArr[x] !0 );
strcpy(suitArr[x],inlineBuf);
}

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,168
#2: Jul 11 '06

re: array of string


And error at compile time or run time?
What was the exact error?
How is inlineBuf declared?
Member
 
Join Date: Jul 2006
Posts: 35
#3: Jul 12 '06

re: array of string


Hi,
The following code can fetch 20 strings each of 19 characters at max from a Ex.txt file in C drive. Copy paste the code n it will work.

#include <stdio.h>
#include <stdlib.h>

void main(void)
{
FILE *f;
char arr[20][20], i=0,j=0;
memset((char*)&arr[0][0], 0, 400);
f = fopen((const char*)"C:\\Ex.txt","r+");

for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
arr[i][j] = fgetc(f);
if(arr[i][j]==10)
{
arr[i][j] = '\0';
break;
}
else if(arr[i][j]==(-1))
{
arr[i][j] = '\0';
break;
}
}
}
printf("%s\n\n",&arr[2][0]);
}
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,168
#4: Jul 12 '06

re: array of string


When posting code if you put it in
Expand|Select|Wrap|Line Numbers
  1.  ... 
then indentation will be preserved.

The casts in the following 2 statements are not required and it is probably better to leave them out.

memset((char*)&arr[0][0], 0, 400);
f = fopen((const char*)"C:\\Ex.txt","r+");


This line of code is open to error

arr[i][j] = fgetc(f);

the prototype of fgetc is

int fgetc( FILE *stream );

but you have imediately assigned it to char demoting it's type. You have no garunttee that EOF will fit in a char (especially if char is unsigned on the platform in question). To ensure correct operation and portability you should assign the return of fgetc to a int variable, check it against EOF and if it is not EOF then you should assign it to a char (if required).

This line is checking for EOF

else if(arr[i][j]==(-1))

you have no garunttee for any given platform that EOF is -1. You should use the defined symbol EOF as this is correct for all platforms.
Member
 
Join Date: Jul 2006
Posts: 35
#5: Jul 12 '06

re: array of string


Yes I agree with you Bafna. But I have struggled with this EOF manytimes becoz it doesn't work manytimes.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,168
#6: Jul 12 '06

re: array of string


corrected

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void main(void)
  5. {
  6.     FILE *f;
  7.     int input;
  8.     char arr[20][20], i=0,j=0;
  9.     memset((char*)&arr[0][0], 0, 400);
  10.     f = fopen((const char*)"C:\\Ex.txt","r+");
  11.  
  12.     for(i=0;i<20;i++)
  13.     {
  14.         for(j=0;j<20;j++)
  15.         {
  16.             input = fgetc(f);
  17.  
  18.             if(input=='\r')
  19.             {
  20.                 arr[i][j] = '\0';
  21.                 break;
  22.             }
  23.             else if(input==EOF)
  24.             {
  25.                 arr[i][j] = '\0';
  26.                 break;
  27.             }
  28.             else 
  29.             {
  30.                 arr[i][j] = input;        
  31.             }
  32.         }
  33.     }
  34.     printf("%s\n\n",&arr[2][0]);
  35. }
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,168
#7: Jul 12 '06

re: array of string


Quote:

Originally Posted by Ashish_CPP

Yes I agree with you Bafna. But I have struggled with this EOF manytimes becoz it doesn't work manytimes.

If you are assing fgetc to a char then it is probably a sign/demotion issue.
Member
 
Join Date: Jul 2006
Posts: 35
#8: Jul 12 '06

re: array of string


Thanks Bafna. I think fgetc() is used to fetch values from a file and no character/value can exceed the limit of -128 to +127 so return value of fgetc can always be transferred to a char variable without loss. I am not sure but I think so.
Reply