The file "airports.csv" contains 12 pieces of info per line - separated by commas:

BIN,"Bamiyan","Bamiyan","Afghanistan","AF",34.800000,67.816667,701,"Afghanistan",\N,\N,1149361
(there are 3 such lines for now)

How do I modify my program to take the 1st, 3rd, 5th,6th,7th pieces of info per line and store them in an array-

ill later use that array to rewrite the file .

#include <fstream>
#include <iostream>
#include "conio.h"
#include <cstring>
using namespace std;



int main()
{
    ifstream ofile;     //open file
    ofile.open("airports.csv");
    
    string dump[12];
    string infoRequired[3][5];
    
    
    //for loop will come here once I/O code is finalised
    ofile>>infoRequired[1][1]>>dump[1];
    
    cout<<infoRequired[1][1]<<dump[1];   //testing to see if pieces of info are copied
    
    
    
    
    getch();
    return 0;
}

Recommended Answers

All 12 Replies

The file "airports.csv" contains 12 pieces of info per line - separated by commas:

BIN,"Bamiyan","Bamiyan","Afghanistan","AF",34.800000,67.816667,701,"Afghanistan",\N,\N,1149361
(there are 3 such lines for now)

How do I modify my program to take the 1st, 3rd, 5th,6th,7th pieces of info per line and store them in an array-

ill later use that array to rewrite the file .

Read a piece at a time from the file. If it's the 1st, 3rd, 5th, 6th, or 7th piece of information, store it in the array. If it isn't something you need to keep, read it in and throw it away. getline could come in useful here, possibly. You can specify a comma or a newline as a delimiter.

here's an example

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	char str1 [10];
	char str3 [10];
	char str4 [10];
	char strFail [10];

	
	ofstream myFile ("C:\\asdf.txt");
	
	myFile << "Helloziewozie my name is";
	myFile.close();

	ifstream theFile ("C:\\asdf.txt");
	theFile >> str1; //get 1st string
	theFile >> strFail; //get 2nd string
	theFile >> str3; // get 3rd string
	theFile >> str4; // get 4th string
	theFile.close();

//printing out 1,3,4th strings.
	cout << str1 << endl;
	cout << str3 << endl;
	cout << str4 << endl;

	system("pause");

	return 0;

}

that's how u get the strings..

just make a new char[10] variable for each string that you want to avoid confusion and make only one char[10] variable that you can use for all the strings that you don't want.. simple :D

Ok I get what ur saying but lets modify this example to

myFile << "Helloziewozie,my,name,is";

How can i specify that the string terminates at comma and not a space

here's an example

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	char str1 [10];
	char str3 [10];
	char str4 [10];
	char strFail [10];

	
	ofstream myFile ("C:\\asdf.txt");
	
	myFile << "Helloziewozie my name is";
	myFile.close();

	ifstream theFile ("C:\\asdf.txt");
	theFile >> str1; //get 1st string
	theFile >> strFail; //get 2nd string
	theFile >> str3; // get 3rd string
	theFile >> str4; // get 4th string
	theFile.close();

//printing out 1,3,4th strings.
	cout << str1 << endl;
	cout << str3 << endl;
	cout << str4 << endl;

	system("pause");

	return 0;

}

that's how u get the strings..

just make a new char[10] variable for each string that you want to avoid confusion and make only one char[10] variable that you can use for all the strings that you don't want.. simple :D

erm... seperate the comma and try..

like

heloziewozie , my , name , is

i guess that way the comma's will be the 2nd 4th and 6th string...

erm... seperate the comma and try..

like

heloziewozie , my , name , is

i guess that way the comma's will be the 2nd 4th and 6th string...

clever!!!
but what if theres no space between the commas !!!

:) !

make the space o,o

if ur writing to the file write with comma spaces :P

cuz i can't seem to think of anything else :O

As I mentioned in my earlier post, you can specify a comma as a delimiter with getline.

As I mentioned in my earlier post, you can specify a comma as a delimiter with getline.

whats the syntax for that?

whats the syntax for that?

C++-style string:

http://www.cplusplus.com/reference/string/getline/


C-style string:

http://www.cplusplus.com/reference/iostream/istream/getline/


Input file:

a,b,ccc,d,ee,f[B]f[/B]
#include <fstream>
#include <iostream>
using namespace std;

int main ()
{
    char inputString[50];
    ifstream ins ("someFile.txt");
    ins.getline (inputString, 50, ',');
    cout << inputString << endl;
    ins.getline (inputString, 50, ',');
    cout << inputString << endl;
    ins.getline (inputString, 50, ',');
    cout << inputString << endl;
    ins.getline (inputString, 50, ',');
    cout << inputString << endl;
    ins.getline (inputString, 50, ',');
    cout << inputString << endl;
    ins.getline (inputString, 50, '\n');
    cout << inputString << endl;
    ins.close ();
    cin.get ();
    return 0;   
}

Thanks!!

One mroe thing .
Is this doable: ins.getline (inputString, 50, ',' || '\n' );

Thanks!!

One mroe thing .
Is this doable: ins.getline (inputString, 50, ',' || '\n' );

When in doubt, try it and find out. No, it won't work. getline takes a char (not a char*) as a delimiter(singular, not delimiters). Look at the cstring class (or string class if you go the string route). If you want to specify more than one delimiter, find a function that specifies a string or char* for the delimiter(s) field. A char can only hold a single delimiter. A string or char* can hold more than one. But you may be looking at a two stage process (read in a line, then split the line into tokens).

..just a note that system("pause") is not a good idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.