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

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 6677
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: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.