473,394 Members | 1,746 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,394 software developers and data experts.

Read and Write to TEXT file.

Im new to c++ and i want to know how to get line by line from the text file and store in to struct. Plz help me.Thanks
Jan 5 '07 #1
2 4192
Banfa
9,065 Expert Mod 8TB
That rather depends on what data is stored in the text file.

Look up the following functions for reading and writing a text file

fopen
fclose
fgets
fputs
fprintf

Once you have a line of text you will need to verify it's format and parse it into your structure.
Jan 8 '07 #2
Ganon11
3,652 Expert 2GB
If you want to use ifstreams and ofstreams instead of fscanf, etc:

1) Make sure you #include <fstream>
2) A file input stream variable is type ifstream, so you can declare an ifstream variable by saying:

Expand|Select|Wrap|Line Numbers
  1. ifstream IN;
3) Open the proper file by using the .open() command:

Expand|Select|Wrap|Line Numbers
  1. IN.open("C:\\My Documents\\My Data Files\\example.txt");
The file name needs to be enslosed in a string like this. Alternatively, if you wanted to open files dynamically (a.k.a. ask the user for a file to open), the variable inside needs to be a cstring.

4) Treat your ifstream variable (IN) like cin - using >>, getline, .ignore(), etc. as you would with the standard istream variable (cin).

5) When you are finished, make sure to close the ifstream variable:

Expand|Select|Wrap|Line Numbers
  1. IN.close();
To write to a file, use an ofstream variable, repeating steps 1 through 3, replacing ifstream with ofstream:

Expand|Select|Wrap|Line Numbers
  1. ofstream OUT;
  2. OUT.open("C:\\My Documents\\My Output Files\\example.txt");
  3. OUT >> "Blah blah blah.  text here." >> endl;
  4. OUT.close();
Treat ofstream variables as you would cout.
Jan 8 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
2
by: Alex | last post by:
Yes you can: <html><head><script language="javascript"> SaveToFile('This is a text to save in a file', 'C:\\temp\\test.txt'); alert(read('C:\\temp\\test.txt')); function SaveToFile (text,...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
4
by: thiago777 | last post by:
I have only 1GB of RAM so I cannot work with files too big with the ReadtoEnd method. Here is the code Im trying so that the file would split in pieces of 256MB: Try Dim BInput...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.