473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

array of string

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
7 10280
Banfa
9,065 Expert Mod 8TB
And error at compile time or run time?
What was the exact error?
How is inlineBuf declared?
Jul 10 '06 #2
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
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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
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

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

Similar topics

8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
11
by: deko | last post by:
I need to create a basic one-dimensional array of strings, but I don't know how many strings I'm going to have until the code is finished looping. pseudo code: Dim astrMyArray() Do While Not...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
2
dlite922
by: dlite922 | last post by:
difficult to explain, easier to show: Here's what my data looks like on the PHP side: Array ( => Array ( => 20003001 => ABC RESTAURANT
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.