By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,232 Members | 1,639 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,232 IT Pros & Developers. It's quick & easy.

Problem with textfile

P: n/a
Hi NG

This kode writes som text in a textfile:

******************************
for (int j=0; j<length; j++)
{
ofstream session_fil("session.txt");
session_fil<<values[j]<<endl;
}
******************************

My problem is when i write 1,3,8 to the file only the 8 number will appear.

Think it is because i dont jump to next line in the textfile when i write to
it.

How do i do that?

Thanks
Nize
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Nize" <ni**@x.xx> wrote in message
news:3f***********************@dread11.news.tele.d k...
Hi NG

This kode writes som text in a textfile:

******************************
for (int j=0; j<length; j++)
{
ofstream session_fil("session.txt");
session_fil<<values[j]<<endl;
}
******************************
You should create the stream outside the loop:

ofstream session_fil("session.txt");

for (int j=0; j<length; j++)
{
session_fil<<values[j]<<endl;
}

Regards, Ron AF Greve

My problem is when i write 1,3,8 to the file only the 8 number will appear.
Think it is because i dont jump to next line in the textfile when i write to it.

How do i do that?

Thanks
Nize

Jul 22 '05 #2

P: n/a
Thanks.

If i start the program now 1,3,8 will be placed i my textfile. If i then run
the program again i need to save the old data, so i will have the program to
jump to the nextline.

This i how i will have it to be:

//First run of the program
1
3
8
//Second run of the program
2
4
5

Can you help again

Thanks again
You should create the stream outside the loop:

ofstream session_fil("session.txt");

for (int j=0; j<length; j++)
{
session_fil<<values[j]<<endl;
}

Regards, Ron AF Greve

Jul 22 '05 #3

P: n/a
In article <3f***********************@dread11.news.tele.dk> ,
Nize <ni**@x.xx> wrote:

If i start the program now 1,3,8 will be placed i my textfile. If i then run
the program again i need to save the old data, so i will have the program to
jump to the nextline.

ofstream session_fil("session.txt");


ofstream session_fil ("session.txt", ios_base::app);

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #4

P: n/a
> >If i start the program now 1,3,8 will be placed i my textfile. If i then
run
the program again i need to save the old data, so i will have the program tojump to the nextline.

ofstream session_fil("session.txt");


ofstream session_fil ("session.txt", ios_base::app);


I then get the errors:

error C2653: 'ios_base' : is not a class or namespace name
error C2065: 'app' : undeclared identifier

Can you help more :o)

Thanks

**************************
All my kode
**************************
#include <iostream.h>
#include <afxwin.h>
#include <fstream.h>

class Protokol
{
private:
CString modtaget;
public:
Protokol(CString modtag)
{
modtaget=modtag;
}

void tilInt()
{
CString data=modtaget; //data = 138
int length=data.GetLength(); // length=3

int *values=new int[length];

for (int i=0; i<length; i++)
{
char chardata=data.GetAt(i); // '1'
int intdata= chardata-'0';//konvertering til int
values[i]=intdata;
cout<<values[i]<<endl;

ofstream session_fil ("session.txt", ios_base::app);
session_fil<<values[i]<<endl;
}

ofstream session_fil("session.txt");

for (int j=0; j<length; j++)
{
session_fil<<values[j]<<endl;
}
}
};
Jul 22 '05 #5

P: n/a
On Sat, 6 Dec 2003 15:56:22 +0100
"Nize" <ni**@x.xx> wrote:
I then get the errors:

error C2653: 'ios_base' : is not a class or namespace name
error C2065: 'app' : undeclared identifier #include <iostream.h> #include <iostream> #include <fstream.h>

#include <fstream>

The nonstandard headers you were including before are not namespace
aware, use the standard versions instead.

Regards,

Matt.
Jul 22 '05 #6

P: n/a
> > #include <iostream.h>
#include <iostream>
#include <fstream.h>

#include <fstream>

The nonstandard headers you were including before are not namespace
aware, use the standard versions instead.

Regards,

Matt.


Ok, but then i get this error:

error C2872: 'cout' : ambiguous symbol

Thanks
Jul 22 '05 #7

P: n/a
On Sat, 6 Dec 2003 16:29:03 +0100
"Nize" <ni**@x.xx> wrote:
#include <iostream.h>

#include <iostream>
#include <fstream.h>

#include <fstream>

The nonstandard headers you were including before are not namespace
aware, use the standard versions instead.

Regards,

Matt.


Ok, but then i get this error:

error C2872: 'cout' : ambiguous symbol


What C++ book are you using? You need to prefix the standard library
provided libraries, functions and types with std:: (or use a "using
namespace std;" declaration).

Regards,

Matt.
Jul 22 '05 #8

P: n/a
In article <20*****************************@linuxfromscratch. org>,
Matthew Burgess <ma*****@linuxfromscratch.org> wrote:
On Sat, 6 Dec 2003 16:29:03 +0100
"Nize" <ni**@x.xx> wrote:

Ok, but then i get this error:

error C2872: 'cout' : ambiguous symbol


What C++ book are you using? You need to prefix the standard library
provided libraries, functions and types with std:: (or use a "using
namespace std;" declaration).


And note that most C++ books that are more than about five years old are
obsolete, because of issues like this. The current C++ standard was
finalized in 1997-98.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.