Streams and Basic File I/O

Creating ASCII data ( rows x columns) from an input file to ofstream (

Page 1 of 1

4 Replies - 805 Views - Last Post: 19 July 2008 - 10:06 AM Rate Topic: -----

#1 ozz_blade  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-July 08

Streams and Basic File I/O

Post icon  Posted 17 July 2008 - 08:10 AM

Task of Program: I want to create this program that runs an input file (test.txt) that contains a matrix with dimensions (11 x11) meaning that there are 11 rows x 11 columns in the file called "test.txt". I want to read the exact dimension into an outfile (ofstream). However, After program is run, the final output is a huge line with all the data combined in the first line (0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0 16 16 16 16 16 16 0 0 0 0 0 0 0....). I am supposed to get the same table that I have in the input file (11 rows x 11 columns), but not a line. I have the right program because it runs perfectly in the output screen, but not when I want to create an outfile ("outdata.txt")What can I Do??
 // Headers
#include <iostream>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <cstddef>
#include <cstdlib>

using namespace std;


// Define variables
typedef float value_type;

int main ()
{
	// Does the file exist?
	ifstream infile;
	infile.open("test.txt");
	ofstream outfile;
	outfile.open("outdata.txt");
	
	value_type data[11*11];
	
	copy(istream_iterator<value_type>(infile),istream_iterator<value_type>(),data);
	
	// Initialize Program
	int n=0,m=0;
	for (n=0; n < 11; n++)
	{
		for (m=0; m < 11; m++)
		{
			outfile << data[(n*11+m)]<< endl;
		}
		std::clog << std::endl;
	}
	infile.close();
	outfile.close();
	return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Streams and Basic File I/O

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,209
  • Joined: 14-February 08

Re: Streams and Basic File I/O

Posted 18 July 2008 - 07:10 PM

dont know if im right but if you want to print out a 2 dimensional array then you should create a 2 dimensional array,

so instead of [11*11] you could use [11][11] pretty sure that should solve ur problem. because the thing is you still just passing in an int even if it is multiplied your still just making a one dimensional array of 121 ints, try this


good luck
Was This Post Helpful? 0
  • +
  • -

#3 Einherjar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 10-February 08

Re: Streams and Basic File I/O

Posted 18 July 2008 - 09:23 PM

View Poststayscrisp, on 18 Jul, 2008 - 10:10 PM, said:

dont know if im right but if you want to print out a 2 dimensional array then you should create a 2 dimensional array,

so instead of [11*11] you could use [11][11] pretty sure that should solve ur problem. because the thing is you still just passing in an int even if it is multiplied your still just making a one dimensional array of 121 ints, try this


good luck


Well, you can split a 1 dimensional array into as many dimensions as you want in theory, it's just easier to look at it as [][].

And see if this works:

for (n=0; n < 11; n++)
	{
		for (m=0; m < 11; m++)
		{
			outfile << data[(n*11+m)];
		}
		std::clog << std::endl;
		outfile << endl; // or outfile << "\n";
	}


Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5909
  • View blog
  • Posts: 12,815
  • Joined: 16-October 07

Re: Streams and Basic File I/O

Posted 19 July 2008 - 02:18 AM

View Poststayscrisp, on 18 Jul, 2008 - 10:10 PM, said:

dont know if im right but if you want to print out a 2 dimensional array then you should create a 2 dimensional array,


Noooo! Um, sorry.

Two dimensional arrays are a bit of a mess, honestly. They're nice enough for a one off but for anything serious I'd take the time flatten the little error prone beasts out. Worst case in a C++ prog is you end up with some funky jagged array that never really gets disposed properly. Pointer pointers and lovely **argv type syntax in C.

Using a standard derived index form (row*cols + col) is simple enough and I'd never advise anyone against it who's already using it.

Anyway, that's not the issue. It looks like the the prior answer of capping it with a new line may be it.
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,209
  • Joined: 14-February 08

Re: Streams and Basic File I/O

Posted 19 July 2008 - 10:06 AM

problem solved :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1