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

testing for end of line from file read

i have an input file with several numbers (ie 73296^4832 /n) carrot being a space and /n meaning theres another line of similar type numbers underneath. i'm trying to read them in one at a time into an array as char type and then converting them to integers, easy enough. the problem i'm having is i need my while loop to stop at the end of each line but i can't figure out how!

i tried creating a bool function within the public section of the class that i'm working on that looked like this:

bool eol(const char& k)
{
if (k == '/n')
return true;
else
return false;
}

and proceeded to implement it in my while loop as follows

ins.get(k);
while (!eol(k))
{
...
}

my compiler does not have an error that keeps it from compiling but it says

[Warning] multi-character character constant
[Warning] In function `bool eol(const char&)':
[Warning] comparison is always false due to limited range of data type

and my limited knowledge of c++ is keeping me from coming up with any alternative! does anyone know what i can do to correct this or a better way of testing for end-of-line?
Jun 25 '07 #1
3 5039
weaknessforcats
9,208 Expert Mod 8TB
Use the >> operator. It skips whitespace. You can use it to read your ints one after the other to the end of the file:

Expand|Select|Wrap|Line Numbers
  1. ifstream input("myfile.txt");
  2. int data;
  3. while (!input.eof())
  4. {
  5.      input >> data;
  6.      cout << data << endl;
  7. }
  8.  
If you really need to process a line at a time, use getline:

Expand|Select|Wrap|Line Numbers
  1. char buffer[256]
  2. input.geline(buffer, 256);
  3.  
then process the buffer to get the ints. Here you might insert the buffer into a stringstream and then extract from the stringstream to your ints:

Expand|Select|Wrap|Line Numbers
  1. stringstream ss;
  2. int a,b,c;
  3. ss << buffer;
  4. ss >> a >> b >> c;
  5. cout << a << " " << b << " " << c << endl;
  6.  
Jun 25 '07 #2
for the buffer example you used, is 256 simply an arbitrary number for your example? i've never used that method but it looks like it might do what i need. the reason i'm reading the numbers in one at a time is so that each individual integer of the numbers (not divided by spaces) is in its own array position.

so i read in the number 7345. array position 0 = 7, array position 1=3...etc.

i have to add the two entire numbers on each line (7345^4567) but one integer at a time and use the carry (add 7+5 carry 2...add 6+4(+2)...etc). thats why i was looking for an end-of-line check reading each integer in one at a time as a char.
Jun 25 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
There is always the option of using cin.get(). That will fetch one byte. It skips nothing. You would need to build your integers from the characters you read but you will see the \n.

All cin.getline() does is wrap a loop around cin.get().

However, if your integers a 2 per line, then you should be able to use >> operator and pull 2 integers at a time.
Jun 26 '07 #4

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

Similar topics

9
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
0
by: Billy Patton | last post by:
I have worked out a testing procedure that works well for me. Here it is for your taking. It has evolved over 15 years of c and perl coding and testing. My perl version looks VERY similar. It...
7
by: Marco | last post by:
I am looking for an open-source or free tool that parses a C header file (.h) for a module and creates a .c file with the functions stubbed out ( ideally with dummy returns for non-void). This will...
22
by: David Warner | last post by:
Greetings! I am working on a C app that needs to read print files generated by lp that contain HP LaserJet PCL codes. If the PCL contains binary data to define a bit map to be printed on the...
2
by: UofFprogrammer | last post by:
I am experimenting with several ways to test for the end of file for an input file .txt If, for example, I have a text (test.txt) file that had: ab 5 fgd 3 fdfe 3 aasa 4 (intentionally blank...
1
thatos
by: thatos | last post by:
I have the class TableCollection which creates tables for data store in the given direcoty, the method which reads this goes like this. public void read(String dirname,String regex) throws...
0
by: Matthew Fitzgibbons | last post by:
I'm by no means a testing expert, but I'll take a crack at it. Casey McGinty wrote: I've never run into this. Rule of thumb: always separate software from hardware. Write mock classes or...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.