8 Replies - 1505 Views - Last Post: 27 March 2009 - 10:50 PM Rate Topic: -----

#1 k2283944   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-November 08

Ran into a snag, reading in from a csv file

Posted 27 March 2009 - 02:19 PM

Thanks to ccubed and jackofalltrades, i got my program working right. BUT the only way that I can get it to work its to add a , at the end of each line but the last in my csv file to get it to process the whole file.

Here is my csv file:

1,3,10,3,1
1,10,10,3,10
1,2,2,2,1
0,0,0,1,10
1,1,0,2,2
10,1,0,1,10

NOTE: There is not a new line in the csv file after the last row of numbers.

loop to read in to array:
  for (int row = 0; row < 6; row++ )
	for (int col = 0; col < 5; col++ )
  {
	infile >> tempint;
	infile >> tempchar;
	
	array[row][col] = tempint;

if(tempint == 10)
	minecount++;
  }



Here is an explanation of whats happening, but I ned to know how to fix this problem.

1 assigned to tempint
, assigned to tempchar
3 assigned to tempint
, assigned to tempchar
etc..

so when it gets to the last value of the first line it reads in 1 to tempint and expects tempchar but gets the next 1 which is on the next line and it will not process the rest of the file.

any help is greatly appreciated

This post has been edited by k2283944: 27 March 2009 - 02:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Ran into a snag, reading in from a csv file

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Ran into a snag, reading in from a csv file

Posted 27 March 2009 - 03:54 PM

Try this: :)
for (int row = 0; row < 6; row++ ) {
	for (int col = 0; col < 5; col++ )
	{
		infile >> tempint;
		
		array[row][col] = tempint;
		
		if(tempint == 10)
			minecount++;

		tempchar = infile.peek();

		switch (tempchar) {
			case ',':
				infile >> tempchar;
				break;
			case 10:
				infile.ignore();
				break;
			default:
				break;
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Ran into a snag, reading in from a csv file

Posted 27 March 2009 - 05:04 PM

Two things you should read about:
istream::getline (various forms - the one I used here lets you specify any character as the delimiter)
and
atoi() (converts a c-string to an integer; this is in cstdlib but it's already #included in iostream)

Using these functions you can read in your array like this (you need to provide a char array for getline):

Edit: erroneous code deleted.  Corrected code is in post #7.

This post has been edited by r.stiltskin: 27 March 2009 - 08:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 k2283944   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-November 08

Re: Ran into a snag, reading in from a csv file

Posted 27 March 2009 - 05:31 PM

n8wsx, I tried your loop with the switch case and I still get the same result. I have included my whole program this time.

NOTE: I am changing the values that are supposed to be my name and my id number since this is a class assignment. It should still run the same though.

This is the output I am getting with both loops:

First Name
123456

Mine Count is: 1

1 3 10 3 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

This post has been edited by k2283944: 27 March 2009 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Ran into a snag, reading in from a csv file

Posted 27 March 2009 - 05:35 PM

Works for me:

Quote

1 3 10 3 1
1 10 10 3 10
1 2 2 2 1
0 0 0 1 10
1 1 0 2 2
10 1 0 1 10
Press any key to continue . . .

#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main(int argc, char * argv[]){
	char tempchar = 0;
	int tempint = 0;
	int minecount = 0;
	int array[6][5];

	ifstream infile ("test.txt" , ifstream::in);

	if (!infile.good())
		cout << "Input file not opened" << endl;

	for (int row = 0; row < 6; row++ ) {
		for (int col = 0; col < 5; col++ )
		{
			infile >> tempint;
			
			array[row][col] = tempint;
			
			if(tempint == 10)
				minecount++;

			tempchar = infile.peek();

			switch (tempchar) {
				case ',':
				case 10:
					infile.ignore();
					break;
				default:
					break;
			}
		}
	}

	infile.close();

	for (int row = 0; row < 6; row++) {
		for (int col = 0; col < 5; col++) {
			cout << array[row][col] << " ";
		}
		cout << endl;
	}


	system("pause");
	return 0;
}

Was This Post Helpful? 1
  • +
  • -

#6 k2283944   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 04-November 08

Re: Ran into a snag, reading in from a csv file

Posted 27 March 2009 - 05:46 PM

Ok, i just re-ran it and it works... i had a copy of the cpp file in the same folder with my original.. thanks for the help i really appreciate it

PS: I'm removing my whole code, so in case a classmate happens upon this post

again, thanks a lot

This post has been edited by k2283944: 27 March 2009 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Ran into a snag, reading in from a csv file

Posted 27 March 2009 - 08:04 PM

I see there was a major glitch in the code I posted earlier, so here's one that actually works. I needed a stringstream to get it to handle both the comma delimiter and end of line with no comma.

The advantage of this is that it can deal with "missing" numbers in the input file, as well as normal input. For example, this data file:
1,,,,
1,10,10,3,10
1,2,2,2,1
0,0,0,1,10
1,1,0,2,2
10,1,0,1,10

puts this in the array:
1 0 0 0 0
1 10 10 3 10
1 2 2 2 1
0 0 0 1 10
1 1 0 2 2
10 1 0 1 10



#include <sstream>
//...
  char buff[50];
  stringstream ss;
  for( int row = 0; row < 6; ++row ) {
	infile.getline( buff,  50 );
	ss << buff;
	for( int col = 0; col < 5; ++col ) {
	  ss.getline( buff, 6, ',' );
	  array[row][col] = atoi( buff );
	  if(array[row][col] == 10)
				minecount++;
	}
	ss << "";
	ss.clear();
  }



Enjoy. Sorry for the misdirection in my earlier post.
Was This Post Helpful? 0
  • +
  • -

#8 CosmicTruth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-March 09

Re: Ran into a snag, reading in from a csv file

Posted 27 March 2009 - 08:34 PM

Fantastic post and great code, I wish you could comment it better because I almost grasp what your doing.
I am trying to do the same thing but my width is = array_width and my array height is also dynamic but = array_height. The file I'm opening is called colors.csv. each time i go through the loops i have to set a bunch of variables and one is coming from the CSV file i need to read, so each pass through the loop i will be doing something like...
sdk_string_set (SDK_OBJECT_ACTION, oa);
and the variable oa I have to read from my CSV file.

I think If i can study this i might be able to absorb it : )

Cosmic
: )
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Ran into a snag, reading in from a csv file

Posted 27 March 2009 - 10:50 PM

A stringstream works essentially the same as an input/output file stream. You declare a stringstream just like an fstream, for example
stringstream ss;
and, like an fstream or cout, you can write to it:
ss << myString; or
ss << myCstring; or
ss << myInt;, or float, or double, etc.

and you can read from it:
ss >> myChar; or
ss >> myCstring; or
ss >> myInt; This is also an easy way to convert strings of digits into ints, floats or doubles.

You can get the entire contents of the stringstream as a single C++ string:
string s = ss.str();

And it also inherits many other members from istream and ostream like get, getline, read, write, put ... In this code I used the stringstream as an intermediate step between the input file and the array to help deal with the fact that the data within each line of the file was separated by commas, but the lines are separated only by newlines.
It uses both forms of the istream getline member. Both of them take characters from the stream, copy them into a char array and terminate them with a '\0'.
The first one reads up to n-1 characters or until it reaches a newline or eof:
istream& getline (char* s, streamsize n );
The second one reads up to n-1 characters or until it reaches the character specified as 'delim' or eof:
istream& getline (char* s, streamsize n, char delim );

#include <sstream>
//...
  char buff[50]; // a buffer to temporarily park the data
  stringstream ss;
  for( int row = 0; row < 6; ++row ) {
	// read a full line of input into the buffer (newline is
	//  automatically discarded)
	infile.getline( buff,  50 );
	// copy the entire line into the stringstream
	ss << buff;
	for( int col = 0; col < 5; ++col ) {
	  // Read from ss back into the buffer.  Here, ',' is
	  //  specified as the delimiter so it reads only until
	  //  it reaches a comma (which is automatically
	  //  discarded) or reaches the 'eof', but of course
	  //  this 'eof' is really just the end of a line of the
	  //  original input.  The "6" is because I figured
	  //  the input numbers would be 5 digits or less.
	  ss.getline( buff, 6, ',' );
	  // Next, use the stdlib atoi function to convert the
	  //  input value that was just read from ss to an int,
	  //  and copy it into the array.
	  array[row][col] = atoi( buff );
	  // This is just something the original poster had to
	  //  do with a special input value for his application.
	  if(array[row][col] == 10)
				minecount++;
	}
	// This copies an empty string into ss, erasing the
	//  previous contents.
	ss << ""; 
	// This clears the 'eof' flag.  Otherwise, even after 
	//  writing new data to ss you wouldn't be able to
	//  read from it.
	ss.clear();
  }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1