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

Join 118,924 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,958 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Streams and Basic File I/O

 
Reply to this topicStart new topic

Streams and Basic File I/O, Creating ASCII data ( rows x columns) from an input file to ofstream (

ozz_blade
post 17 Jul, 2008 - 08:10 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??
CODE

// 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;
}
User is offlineProfile CardPM

Go to the top of the page


stayscrisp
post 18 Jul, 2008 - 07:10 PM
Post #2


D.I.C Head

**
Joined: 14 Feb, 2008
Posts: 231



Thanked 5 times
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Einherjar
post 18 Jul, 2008 - 09:23 PM
Post #3


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


QUOTE(stayscrisp @ 18 Jul, 2008 - 10: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


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:

CODE

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";
    }
User is offlineProfile CardPM

Go to the top of the page

baavgai
post 19 Jul, 2008 - 02:18 AM
Post #4


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,792



Thanked 73 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


QUOTE(stayscrisp @ 18 Jul, 2008 - 10: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,


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.
User is offlineProfile CardPM

Go to the top of the page

stayscrisp
post 19 Jul, 2008 - 10:06 AM
Post #5


D.I.C Head

**
Joined: 14 Feb, 2008
Posts: 231



Thanked 5 times
My Contributions



problem solved smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/13/08 05:15AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month