473,472 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to input values from file

8 New Member
Input file looks like this:

5 Christine Kim...... 30.00 2 1 F
15 Ray Allrich 10.25 0 0 M
16 Adrian Bailey 12.50 0 0 F

with exactly 20 characters from the start of the name to the null before the double.

using
Expand|Select|Wrap|Line Numbers
  1. int empNum,
  2.         empDep,
  3.         empTyp;
  4.     char empNam[MAX_CHARS];
  5.     double empRat;
  6.     ifstream inEmp;
  7.  
  8.     inEmp.open("master7.txt");
  9.     if (!inEmp)
  10.     {
  11.         cout << "\n\nError opening the file!!" << endl;
  12.         exit(1);
  13.     }
  14.  
  15.     inEmp >> empNum;
  16.     inEmp.ignore(1, '\0');
  17.     inEmp.getline(empNam, MAX_CHARS); 
  18.     inEmp >> empRat >> empDep >> empTyp;
  19.     cout << "\nNum: " << empNum << "\nName: " << empNam[0] << empNam[1] << empNam[2]
  20.     << "\nRat: " << empRat << "\nDep: " << empDep << "\nType :" << empTyp;
  21.     system("PAUSE");
  22.  
  23.  

my output is as follows:
Num: 5
Name: Chr
Rat: -9.25596e+61
Dep: -858993460
Type :-858993460Press any key to continue . . .


why can't I get a good read for my double empRat?
May 14 '10 #1
7 2387
BenFedorko
8 New Member
@BenFedorko
Well I figured that part out.

after I input the array of characters i used the "inEmp.clear();" function like this:

Expand|Select|Wrap|Line Numbers
  1.     inEmp.ignore();
  2.     inEmp.getline(empNam,(MAX_CHARS));
  3.     emp[empCount].setName(empNam);
  4.     inEmp.clear();
  5.  
I still don't know why there was anything IN the buffer when I was done, maybe I need to use the cin.get() instead of cin.getline().
May 15 '10 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
5 Christine Kim...... 30.00 2 1 F


From this, there is:

char 5
char space
char C
char h
char r
char i
char s
char t
char i
etc...
char 3
char 0
char .
char 0
char 0
char space

Where is the double and the int?

Text files are char only. You will need to parse 30.00 as a string and convert that string to a double.

Your only other choice is a binary file. There you can have the int and the double.

However, you use getline that that only works with text files.
May 15 '10 #3
BenFedorko
8 New Member
if I use "fin" as my file input stream I should be able to gather all of the data from this line using:

Expand|Select|Wrap|Line Numbers
  1. fin >> integerNumber;   // where this is an integer
  2. fin.getline(name, 20);    // character array
  3. fin >> doubleNumber;   // double
  4. fin >> integerNumber2; // integer
  5. fin >> integerNumber1; // integer
  6. fin >> characterF;        // character
  7.  
for some reason there was bad data in the buffer after I got the character array, once I used the inf.clear() function my other inputs worked just fine. I want to know why I need this function!
May 21 '10 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
If this is your data:

5 Christine Kim...... 30.00 2 1 F
15 Ray Allrich 10.25 0 0 M
16 Adrian Bailey 12.50 0 0 F

then the fin >> integerNumber fetches the 5 and leave you positioned at trhe space right after the 5 and before the C. This is where your fin.getline(name, 20) will start. That means your 20 will stop one byte short of the 3. This is where
fin >> doubleNumber will start. Since this position may be a text character and and not a double format, the >> will set the stream failbit.

From this point on, every >> will fail becuse the the first thing >> does is check that failbit.

You need to a) know where you are exactly in that inout record or b) reformat the record:

5Christine Kim......30.00 2 1 F
-<exactly 20 bytes..>

Then after each >> operation, I would verify the failbit is not set. If it is, you need to reset the bit (the clear()) and then remove the offending data or the bit just gets set again:

Expand|Select|Wrap|Line Numbers
  1. fin >> integerNumber;
  2. if (fin.fail() == true)
  3. {
  4.     //take corrective action or terminate the program
  5. }
Personally, I would not use the getline(). Instead I would use >> to a string object. This will pick up the first name and stop at the space after the first name. Then I would use the another >> to pick up the last name.

Remember the >> skips all whitespace characters.

This means I would keep these strings separate as first name and last name.
May 21 '10 #5
BenFedorko
8 New Member
@weaknessforcats
Thank you for your explanation that makes a lot of sense, I tried using the ignore() function but that didn't seem to work. According to your theory it should have though right?


You said:
"then the fin >> integerNumber fetches the 5 and leave you positioned at trhe space right after the 5 and before the C. This is where your fin.getline(name, 20) will start. That means your 20 will stop one byte short of the 3. This is where
fin >> doubleNumber will start. Since this position may be a text character and and not a double format, the >> will set the stream failbit."

but if after the
"5 Christine Kim...... 30.00 2 1 F"
line after the last "." is accepted and the cursor is to the left of the null character before "30.00," then a fin.ignore() or fin.ignore(\0) should behave like your clear function and my next fin should input the "30.00" properly. But it did not, do you have any ideas why?


Also, this is for an assignment and so the professor asked us to keep all strings as arrays of characters (c-strings) and so I needed to input in that way. If there is a better way to fill the array with the input file information, by all means show me how! :) And of course I cannot change the input file either because of class restrictions.

Thanks again.
Jun 1 '10 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
If you need to use arrays of chars, then I would use cin.get() and fetch the name one byte at a time and store the byte in an element of the char array.

Don't forget to add the \0 in the arrayfollowing the last byte of the name. That will make your array a C-string.

If your name is guaranteed to have a non-text character after the first and last name, then you can use the >> operator with a char array for the first name and another char array for the last name.

This is the same as using string objects but with no protection of the arrays.
Jun 2 '10 #7
BenFedorko
8 New Member
@weaknessforcats
Interesting. Thank you for the information, I appreciate it very much.
Jun 2 '10 #8

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

Similar topics

3
by: Oxygenearth | last post by:
Please who could help me with this... I had my structure in Win32, with Apache, PHP, and MySQL, I had a page in which I am transfering an image to the database in MySQL using PHP. But now I am...
11
by: TechNovice | last post by:
Hi: I'm trying to find a way to test input values. To test an integer I tried this code: ******Code****** int input_number; cin>>input_number; while(!input_number) cout<<"invalid...
11
by: TJM | last post by:
Hi, A Javascript error is generated when the user types a few character in an INPUT TYPE=FILE and hits a submit button. The form does not post. Is there a solution to this problem? Thanks TJM
1
by: bonzo | last post by:
Hi I have external exe file that reads some data from a file named input.txt. I want to detect that attempt and push my string in the input. Is there any way how to do it without reading/writing on...
5
by: hrpreet | last post by:
Hi All, I need the file chooser in the jsp, just for brosing and saving the file path in the database, so i have used the following code.I dont need to read the file content. I have to make it...
4
by: SammyBar | last post by:
Hi all, I wonder is it possible to upload the content of an <imgfield to a server. The content of the <imgwas downloaded from a web site different from the one it should be uploaded. The image...
2
by: mypublicmail | last post by:
I'm moving big chunks of html into and out of divs using innerHTML. Or, I thought I was until I tested it on Firefox 1.5. Firefox will move the html just fine, but if you have changed any input...
12
by: Larry Bud | last post by:
I rarely crosspost, but this affects both ASP and Javascript REALLY odd bug that I ran across in ASP 3.0. I have an input type of file, user clicks browse, then places his cursor in the...
11
by: waffle.horn | last post by:
Hi, if this makes sense i want to create a function that can be called so that it reads a single line from a file, then after using the information destroys it. Such that when the function is...
21
by: dadimar | last post by:
I'm trying to write a program that reads unspecified number of positive and negative values, counts them and computes the average of the input values, not counting zeros. The program should end with...
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
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
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...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.