Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

C++ File Reading

Original Message
Name: SarumanTheWise
Date: February 13, 2008 at 11:56:10 Pacific
Subject: C++ File Reading
OS: Windows XP
CPU/Ram: AMD Athlon 64 1024MB RAM
Model/Manufacturer: Compaq Presario
Comment:
I'm trying to make a relatively simple program but I can't remember what to do to get it to work. What I basically want it to do is read in information from an external file that contains characters and numbers, like (44, 12)\n , of course there's a bunch of them in a row so that's why I put that \n there. I don't care for those characters, I just want to put the first number in a variable and the second number in another. Now, I'm not sure what I'm doing wrong. I've been trying to start off by doing:

ifstream infile;
infile.open("file.txt");
while( infile )
{
infile >> x;
if ( x == '(' )
infile >> x;

...

}

Am I using the wrong method? I'm obviously no expert at this yet, I've read many tutorials on file reading but none showed how to exclude non-integers. I want the reader to skip the characters and just continue reading in and store that number in a variable for later calculations. I don't want to use getchar() that would overly complicate things. I've thought about getline() but I don't know how that thing works, does it skip spaces or characters? I don't know. I'm not totally sure what I'm doing wrong, I thought originally what I was doing was right but my program just hangs and does nothing.



Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: February 13, 2008 at 20:30:17 Pacific
Subject: C++ File Reading
Reply: (edit)
Am I using the wrong method?
Probably; it depends on the type of x.

my program just hangs and does nothing.
I'm kinda surprised it hangs; the loop should break after the first pass.

If x is an int, as I assume it is, then the next bit of text you read better be convertible into an int. Obviously, ( cannot.

What to do about this? Personally, I'd make a function, like this:

int readVal(istream &in) {
int x = 0;
//We move forward, looking for digits...
while (in && (unsigned)(in.peek() - '0') > 9)
in.ignore();
//If in.eof(), this will fail, and we'll return 0;
in >> x;
return x;
}

Then I'd modify your given bit of code to read like this:
ifstream infile("file.txt");
//If we got a non-zero value into x, !infile.eof()
//Or, if x = 0, continue to loop if infile.good().
while (x = readVal(infile) || infile) {
. . . .
}


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C++ File Reading

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




how to setup call of duty to joytok

WindowsME / HotMail Problem

Corrupt memory

Convert fat32 to Ntfs

Best WinMo phone of 2008


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC