473,471 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

read line

Hi,

I have a text file I will read it and write out binary. The file includes
256 coloums.

I use

while (infile.good())
{
infile.getline (buffer,2200);
cout<<buffer<<endl<<endl;
exit(0);
}

so I can see the file.

Each field is seperated by 1 TAB or 2 TABS I mean \t.

Is there a way to read this file at once into an array, line by line. They
are all numbers.

Regards...
Jul 23 '05 #1
2 6680
long double ld=50;
printf("%Lg",ld);

and display show -6.8065E+038
no 50
Jul 23 '05 #2
kak3012 wrote:
Hi,

I have a text file I will read it and write out binary. The file includes 256 coloums.

I use

while (infile.good())
{
infile.getline (buffer,2200);
cout<<buffer<<endl<<endl;
exit(0);
}

so I can see the file.

Each field is seperated by 1 TAB or 2 TABS I mean \t.

Is there a way to read this file at once into an array, line by line. They are all numbers.


It's not entirely clear whether you want an array of numbers, or an
array of strings, each representing a single line from the file. For
an array (or technically, a vector, but you rarely want an array
anyway) of numbers, you could do something like this:

typedef double type; // use type appropriate to your numbers.

std::vector<type> numbers;

std::copy(std::istream_iterator<type>(infile),
std::istream_iterator<type>(),
std::back_inserter(numbers));

If you want the numbers kept as strings, but still want each as an item
in the vector, you can just change 'type' from 'double' to
'std::string'.

Oddly enough, if you want each LINE as a string in an array, it's a
little more difficult (though not much). You can do it a little like
above by creating a class that acts like a string, except that
operator>> reads a whole line instead of a single token. It's usually
easier, however, to do the job explicitly, with something like:

std::vector<std::string> numbers;
std::string temp;

while(infile.good()) {
std::getline(infile, temp);
numbers.push_back(temp);
}

A few notes about C++ in general: as mentioned above, especially to
start with, you probably won't encounter many reasons to use arrays --
you'll usually want to use vectors instead (or std::string instead of
arrays of char). Going along with that, you rarely want to use
istream::getline -- you'll almost always want to use std::getline
instead, because you'll be reading into a string instead of an array of
char. The first bit of code I have above probably looks rather foreign
to you right now, since you've probably never seen or used those parts
of the standard library yet -- but studying them is an effort that will
be well worthwhile, at least IMO. You can certainly read doubles (for
example) into a vector with code much like the second piece above --
but I think it's worth the work to learn how to do it the first way
I've given above instead.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Chuck Anderson | last post by:
Can anyone point me in the right direction? I want to use Php to automate confirmation of someone joining an email list by them replying to an email (so they don't have to have a browser?). I...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
4
by: yo_mismo | last post by:
Hi everyone, I'm trying to read the first line of a file this way: .... .... .... .... new_line=0; while((read=read(fd, &info, sizeof(info))) > 0 && !new_line){ if (strcmp(&info, "\n") !=...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
6
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: ...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.