473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with textfile

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
8 1315

"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
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
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
> >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
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
> > #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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
2
by: Suchi | last post by:
Hi all: I want to read a textfile from an ASP program. My program is like this: Set fso = CreateObject("Scripting.FileSystemObject") workFile=http://localhost/Readme.txt) Set textFile =...
1
by: Larry Rekow | last post by:
I have a report that's created each day as a flat textfile. Because I came from the Access world, I created a macro that imports it with a schema that gives meaningful names to the various...
4
by: Federico Bari | last post by:
Good morning all from italy, i have probably a compatibility problem with a html/javascript page. The aim of the code of the file test.htm you find here following (copy the 3 files in the...
1
by: Michael Schindler | last post by:
Ich habe es nun erreicht, ein textfile einzulesen das nun zeile für zeile im datagrid angezeigt wird. Nun wäre es toll wenn ich das textfile zum beispiel nach jedem blank in eine spalte...
3
by: michael haller | last post by:
In my project i have a textfile witch i import in my c# application. The datas in the textfile i show the datas in the textfile in my datagrid..ok that ist not my problem. Now i have a...
1
by: wadacom | last post by:
I'm sorry to take your time for newbie problems but I've been searching what to do about the problem with my apache server I have. I work with ubuntu dapperdrake I put the last apache server on it...
3
by: saytri | last post by:
I am displaying the contents of a textfile in a textArea. I want that when i edit this text in the textArea it is automatically saved in the textfile. I have wriitten this code, but the problem is...
1
by: perdoname | last post by:
Hello, Im trying to implement a program which will split a text file and then parses the elements to an arraylist. My text file looks like that: My program is that: public class Parse {
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.