Problem with writing char array to a textfile

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2069 Views - Last Post: 03 November 2012 - 06:37 AM Rate Topic: -----

#1 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Problem with writing char array to a textfile

Posted 02 November 2012 - 08:45 AM

Hello,

I'm having a absolute nightmare at the moment. I'm trying to write a char array to a text file. This works fine but I want to be able to format so that each element is on a different line. At the moment it is all on one line in the text file. I have tried different things such as putting:
file << parent[i] << endl; 



But all this does is print out random symbols or whatever they are which are not supposed to be. The working code is just printing the correct data in one line, which is below:
void writeToFile()
{
        ofstream file;
        file.open("test.txt", ios::app);
        file << "\n" << endl;
        for(int i = 0; i < arraySize; i++)
        {
                file << parent[i];
        }
        file.flush();
        file.close(); 
}



Thanks,
Mark

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with writing char array to a textfile

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Problem with writing char array to a textfile

Posted 02 November 2012 - 08:54 AM

show us the actual code from what you tried.
Was This Post Helpful? 0
  • +
  • -

#3 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Problem with writing char array to a textfile

Posted 02 November 2012 - 09:01 AM

View Postishkabible, on 02 November 2012 - 08:54 AM, said:

show us the actual code from what you tried.


void writeToFile()
{
        ofstream file;
        file.open("test.txt", ios::app);
        for(int i = 0; i < arraySize; i++)
        {
                file << parent[i] << endl;
        }
        file.flush();
        file.close(); 
}



This outputs ਊਠਠਠਠਠਠਠਠਠਠਠਠਠਠਠਪਠਠਠਠਠਠਠਠਠਠਠਠਠਠਠਊ

This post has been edited by crapmyster: 02 November 2012 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Problem with writing char array to a textfile

Posted 02 November 2012 - 09:53 AM

What is the content of arraySize and parent?
Was This Post Helpful? 0
  • +
  • -

#5 Rahul69  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 34
  • Joined: 10-October 12

Re: Problem with writing char array to a textfile

Posted 02 November 2012 - 11:32 AM

@crapmyster Maybe the file is written in binary mode, have to tried to read from the file and print on console?
Was This Post Helpful? 0
  • +
  • -

#6 quim  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 182
  • Joined: 11-December 05

Re: Problem with writing char array to a textfile

Posted 02 November 2012 - 11:37 AM

The concept is good. Your problem might be with the arraySize variable. you either did not initialize it or assigned a value that is more than the string character, so this is why you get weird/random value when you output to the string.
you must declare/define arraySize and parent, unless it is a global variable in which you should initialize it.

try this:
void writeToFile()
{

        int arraySize = 11;
        char parent[arraySize] = "Hello World";
        ofstream file;
        file.open("test.txt", ios::app); /*notice that ios::app is the append mode, so when you write to the file it will append*/
        /*file << "\n" << endl; // take this out it must be inside the loop but you probably only one one line*/
        for(int i = 0; i < arraySize; i++)
        {
                file << parent[i] << "/n"; // add newline between each character
        }
        file.flush();
        file.close(); 
}


Was This Post Helpful? 0
  • +
  • -

#7 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Problem with writing char array to a textfile

Posted 02 November 2012 - 01:44 PM

This is defined as global:

const int arraySize = 31;
char parent[arraySize];


Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4062
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: Problem with writing char array to a textfile

Posted 02 November 2012 - 01:58 PM

And where did you ever fill in your array? Show us the smallest complete program that illustrates your problem.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Problem with writing char array to a textfile

Posted 02 November 2012 - 02:00 PM

Let me simplify. As a beginner, don't use globals. That's a ham-fisted principle, but as a beginner, you are unlikely to use globals correctly.
Was This Post Helpful? 1
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1073
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Problem with writing char array to a textfile

Posted 02 November 2012 - 03:08 PM

This sounds like a combination of you not having any data in your character array, as well as trying to output a non-null terminated string.
Was This Post Helpful? 0
  • +
  • -

#11 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Problem with writing char array to a textfile

Posted 03 November 2012 - 03:22 AM

Just to clear up there is characters in the array. An combination of '*' and ' ' (spaces). The array is being outputted to a text file, however in a straight line. I'm trying to output it in the text file so that each element is on a new line.

#include <iostream>
#include <fstream>
using namespace std;
const int arraySize = 31;
char parent[arraySize];
char child[arraySize];

void initilise()
{
        for(int i = 0; i < arraySize; i++)
        {
                parent[i] = ' ';
                child[i] = ' ';
        }
        parent[15] = '*';
        
}

void writeToFile()
{
        ofstream file;
        file.open("test.txt", ios::app);
        for(int i = 0; i < arraySize; i++)
        {
                file << parent[i];
        }
        file.flush();
        file.close(); 
}



Above is the code that working with.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Problem with writing char array to a textfile

Posted 03 November 2012 - 03:38 AM

This is like pulling teeth.

Show us ALL of your code. If you don't actually call initilise(sic), your arrays won't be initialized.
Was This Post Helpful? 0
  • +
  • -

#13 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Problem with writing char array to a textfile

Posted 03 November 2012 - 03:45 AM

sorry, forgot to include:



#include <iostream>
#include <fstream>
using namespace std;
const int arraySize = 31;
char parent[arraySize];
char child[arraySize];

void initilise()
{
        for(int i = 0; i < arraySize; i++)
        {
                parent[i] = ' ';
                child[i] = ' ';
        }
        parent[15] = '*';
        
}

void writeToFile()
{
        ofstream file;
        file.open("test.txt", ios::app);
        for(int i = 0; i < arraySize; i++)
        {
                file << parent[i];
        }
        file.flush();
        file.close(); 
}

void menu()
{
        while(true)
        {
                cout << "" << endl;
                cout << "Press 's' to Start" << endl;
                cout << "Press 'e' to Exit" << endl;
                cout << "Enter a choice:" << endl;
                char input;
                cin >> input;
		switch(input)
		{
			case 's': initilise();
                                  writeToFile();
				  break;
                        case 'e': 
                                  break;
			default:
				  cout << "Error: Wrong selection, choose again" << endl;
				  break;
		}
        }
}

int main()
{
    menu();
    return 0;
}




Was This Post Helpful? 0
  • +
  • -

#14 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Problem with writing char array to a textfile

Posted 03 November 2012 - 04:43 AM

Works fine for me (except exiting your menu doesn't work).

What compiler are you using?
Was This Post Helpful? 1
  • +
  • -

#15 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 12-October 11

Re: Problem with writing char array to a textfile

Posted 03 November 2012 - 06:06 AM

View PostJackOfAllTrades, on 03 November 2012 - 04:43 AM, said:

Works fine for me (except exiting your menu doesn't work).

What compiler are you using?


Hey, I've been using the NetBeans IDE with some compiler which I think is from Cygwin. I booted in Linux and used the gcc compiler and I get the right output to the file. So the netbeans compiler is being gay. If someone can explain why it is not working how it should in netbeans and it is in Linux would be much appreciated.
Also, note exiting the menu won't work as I have not fully completed that.
Thanks, everyone.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2