Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple file access in C++

Status
Not open for further replies.

gaus

Programmer
Feb 5, 2001
92
US
Hi,
I am brand new to C++ and am coming from a decent amount of experience with VB. I am trying to find the combination of statements to declare and open a sequential file (local), then read each record into a variable so I can then check for data within certain portions of that variable (substr). So far, I have found no equivalents to performing this in VB. Does anyone out there have the sequence of commands for:
Opening a file called empfile.txt (in current directory, or coded location like the A:\ drive)
Looping thru the file, reading each record looking for a match on data entered by the user and captured in a variable.

Any help would be appreciated. This seems to not be nearly as friendly or intuitive as VB.

Thanks much,
Gary
 
#include <fstream.h>

ifstream inFile;
char buf;

inFile.open(&quot;c:\\empfile.txt&quot;);
while (!inFile.eof())
{// if each line is built the same
inFile>>var1>>var2>>var3>>buf;
//now buf should equal '\n', and a new line should start
//if you want to read a line at once use getline()
//do something
}
inFile.close();

Hope this helps you to get started

Branko
 
Indeed it does - thanks! Hey, is there a C++ equiv to the IsNumeric() function in VB? So far I have not found an easy way to validate numric input...
Thanks again,
Gary
 
Hi,

check the functions atoi(), atol() and atof() and their return values.
Btw. you can read numeric values directly from a text file ninto numeric variable types

int ivar1, ivar2;
char cvar[20];

infile >> cvar >> ivar1 >> ivar2;

Branko
 
Great information!
Thanks again - I appreciate it.
Gary
 
Sorry - one more thing. I am not quite clear as to the buf. Is that a char indicating delimiting, and is it the ascii representation? Or, is it the length of the record being read?
Thanks,
Gary
 
I guess what I need to know is this. How do you tell the compiler that when you read a file, that this is how it is laid out:
type name length
int field1 6
string field2 30 (e.g. first mi last)
string field3 20
float field4 10 ($ value with no explicit '.')

How does it know that the first field is a 6 byte int field, the 2nd is a string (name), the 3rd is a string (title), the 4th is a salary? How does it know how ti delimit or parse the data and place the proper data in the proper variable (where the data begins and ends for each field)?
Thanks,
Gary

 
If you use >> var1, var1 will be read till the next white space. if var1 is declared as a numeric value it will try to convert what it reads to a numeric value.
If what you are reading is a text file, you could do :
int field1;
string field 2, field 3; (or char field2[20]...)
char c;
infile >> field1 >> field2 >> field3 >> field4 >> c;
c should now hold the newline character ('\n')
If you are using a non-ascii format file it is a different story.

Branko
 
Thanks for the response. I'll try. It seems, though that I already tried the method of char field[20], and it still stopped at the white space. I'll try again and make sure.
When it tried to read the name
John W Doe
the field ended up with John...
I'll make sure that I try this though.
Thanks,
Gary
 
Hi Gary,

When you read from a text file using >> it will stop at the first whitespace it encounters, that is correct. You can avoid this by using getline(), this will read the whole line at once. After this you will have to parse the string. You can use for instance the (C)String functions Find to search for substrings (like spaces or tabs '\t') and Left Mid and Right to extract the values. Since you know the length of the char fields you can extract exactly the right length.
Another possibility is to use getline and define a delimiter character. This is standard '\n', you could use '\t' if your file is tab delimited.
example:

int field1;
char field2[30];
char field3[20];
float field4;
char c;

inFile >> field1 >> c; //c should now hold '\t'
inFile.getline(field2, 30, '\t');
inFile.getline(field3, 20, '\t');
inFile >> c >> field4 >> c; // c should now contain '\n'

try it, I cannot check it now

Branko
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top