"Jon Bell" <jt*******@presby.edu> wrote in message
news:bq**********@jtbell.presby.edu...
In article <1070598167.460405@sj-nntpcache-5>,
Venkat <ve*******@yahoo.com> wrote:
I need to copy strings from a single dimensional array to a double
dimensional array.
My advice is to use a vector of strings instead of a two-dimensional array
of chars, and C++ style I/O.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
main()
int main()
{
char Single[255];
int i=10;
char Double[255][255];
string Single;
vector<string> Double(10); // you need only ten strings, right?
for(int j=0; j<10; j++)
{
printf("Enter a string\n");
scanf("%s",Single);
cout << "Enter a string\n";
getline (cin, Single); // assuming you want an entire line
// including blank spaces //Here i need to copy the input String into Double........
Double[j] = Single;
}
//Here i need to display the 10 string thus inputted by the user in the
above loop using the Double array.
for (int j = 0; j < 10; j++)
{
cout << Double[j] << '\n';
}
return 0;
}
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Hi Jon,
Thanks for the timely response. But let me tell you my requirement.
I am reading line by line from a file and than parsing the each read line to
get a particular string and than need to store them.
Here is the text file
File1.txt
The version number is 3.3
Table Countries
{4534-6576-4234-3421ACD},FRANCE,True,321
{4353-7564-4353-5345AFB},UK,False,546
{4234-7678-4534-5645CBD},ITALY,True,675
Table Capitals
{1234-5678-3930-4567-392ACD}, PARIS,True,456
{4567-3432-7899-3930-4567EAC},LONDON,False,560
{7384-6970-3094-2034-3423CAF},ROME,True,394
Table Names
{2435-5435-7676-4546-123434}, John,True,566
{4543-2345-2345-4565-456565},Julie,True,565
{3454-4566-4565-1234-454545},Lucie,False,766
Now from the able file i need to extract the capitals i.e. PARIS, LONDON and
ROME and place them in a vector as you said and display them.
I used file operations to extract them and store them in a character array
using pointers.
Here is my app
// CSVRead.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <Winsock2.h>
using namespace std;
int main(int argc, char* argv[])
{
char File1[255];
char line[255];
char *subline;
char *tag;
char *ddi;
char tagarray[255];// for storing each capital and displaying it
char tableCapitals[22] = "Table Capitals";
char tableNames[30] = "Table Names";
strcpy(CSVFile,"C:\\File.txt");
fflush(stdin);
FILE * fp1 = fopen(File1, "r");
// Assuming this Code will take the file pointer to the location where Table
Capitals is located
while(fgets(line,22,fp1) != NULL)
{
if(!stricmp(tableTag,line))
break;
}
//to skip 1 line.
fgets(line,255,fp1);
//File pointer is pointing to the line after Table Capital
while(fgets(line,255,fp1) != NULL)
{
//Break when the string Table Names is in the line read from the file1
if(strstr(line,tableNames))
break;
// This code fetches me the capitals and i can print them using
tagarray.
subline = strchr(line,',');
tag = tagarray;
if(subline == NULL)
break;
//Copying from subline to tag(pointers)
while(*subline != '\0')
{
if(*subline == ',')
subline++;
*tag = *subline;
subline++;
tag++;
if(*subline == ',')
break;
}
*tag = '\0';
printf("The capitalis %s and its length is
%d\n",tagarray,strlen(tagarray));
}
fclose(fp1);
return 0;
}
Is it possible to use string and vector in this case instead of single and
double dimensional array. If possible could please tell me how to go about
it.
Regards,
Venkat