By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

array of string

P: 1
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);
}
Jul 7 '06 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
And error at compile time or run time?
What was the exact error?
How is inlineBuf declared?
Jul 10 '06 #2

P: 35
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]);
}
Jul 12 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
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.
Jul 12 '06 #4

P: 35
Yes I agree with you Bafna. But I have struggled with this EOF manytimes becoz it doesn't work manytimes.
Jul 12 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
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. }
Jul 12 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
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.
Jul 12 '06 #7

P: 35
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.
Jul 12 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.