Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,047 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,556 people online right now. Registration is fast and FREE... Join Now!




Basic I/O Stream

 
Reply to this topicStart new topic

Basic I/O Stream, Printing data from input file to output file. (please help)!

ozz_blade
17 Jul, 2008 - 11:13 AM
Post #1

New D.I.C Head
*

Joined: 17 Jul, 2008
Posts: 10

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??

Example of the inputfile: ("test.txt"): 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0
0 0 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 0
0 0 1 1 1 1 1 0 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0
Example of the outputfile ("outdata.txt") : 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 0 0 1 1 1 .......0 0 0 0 0
/ Headers
cpp
#include <iostream>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <cstddef>
#include <cstdlib>

using namespace std;


// Define variables
typedef float value_type;

int main ()
{

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;
}

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Basic I/O Stream
17 Jul, 2008 - 11:45 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,522



Thanked: 96 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Please don't double post, and please code.gif

If you don't follow those two key rules, people tend to be less likely to help you out.

Threads merged.
User is offlineProfile CardPM
+Quote Post

F!st!cuffs
RE: Basic I/O Stream
17 Jul, 2008 - 04:18 PM
Post #3

D.I.C Head
**

Joined: 15 Jul, 2008
Posts: 55



Thanked: 2 times
My Contributions
After your inner loop completes you could

cpp

outfile << "\n";

which will insert a newline into your text file
User is offlineProfile CardPM
+Quote Post

ozz_blade
RE: Basic I/O Stream
17 Jul, 2008 - 11:00 PM
Post #4

New D.I.C Head
*

Joined: 17 Jul, 2008
Posts: 10

Hey thanks!!! F!st!cuffs it really works know. I actually added :
outfile << "\t", and outfile << "\n"; after the inner loop.
Thanks for your idea, really!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:22PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month