reading string from file

i need to read in a name from a file along w/ other data but i'm getting a compiling error when reading in the string.

error:  no operator "<<" matches these operands
operand types are: ifstream << char *

char *custName;
char transCode;
char payment;
int  time;

while(custFile << custName << transCode << payment << time)
{
}
capnjazzAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
basantConnect With a Mentor Commented:
My friend,

(1 )For reading you have to use
>> operator.

(2) There is another possiblity
use string class for that

string custName;
char transCode;
char payment;
int  time;
Your example :

while(custFile >> custName >> transCode >> payment >> time)

(3) if u still want a >> operator for a char* you can easily write that.

0
 
capnjazzAuthor Commented:
also,
error: an array may not have elements of this type.

char types[5][] = { "Arrival", "SignIn", "Renewal", "Register", "Cashier" };
0
 
nietodCommented:
>> use string class for that
the "char *" type of strings that are used in C are very problematic.  They can't be copied using = they can't be compared with == (but in both cases the code will compile,, it just won't work as expected) the strings can't be passsed or returned by value, it is dangerious to change the strings to new values unless you are very careful not to overflow the array used to store the string.   These and other problems make "char *" strings the source of the majority of bugs in C programs.  Fortunately C++ has classes and this allows the creation of C++ classes that eliminates these problems.  Unless you must maintain compatibily with C, you should almost never use "char *" strings, you should use string classes instead.  The resulting program will be safer and probably faster too.  The STL provides a string class called "string" and there are others available in other libraries, or you can even write your own.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rob_dragCommented:
Hello!!!

When you read a string you can not use the ">>" operator. This read only ONE character.

You have to use other commands like......cin.get(name, 30);
here you fill name with max 30 characters.

But for reading from a file you have to use the ifstream operator, like this....

infile ifstream("filename.txt");

if (infile)  {  //if the file exists...
  while (!infile.eof()) {
    infile.cin.get(custname, 30);
    infile.ignore();
    infile.get(transcode, 30);
    .....
    ....
    }
}




Robert

0
 
nietodCommented:
>> When you read a string you can not use the
>> ">>" operator. This read only ONE
>> character.
It depends on the stream's settings, but usually it just reads 1 word, not one character.  

>> use other commands like......cin.get(name, 30)
You would be much better off using a string class and using the global getline procedure.
0
 
rob_dragCommented:
Hello!!

If you want to read a word(more than one character), you have to use the "cin.get" operator. When you reading from a file, you can also use the "getline" procedure. Personaly i'll always use the "get" procedure. Then you have to have ignore procedure after. I'm not sure that you have to use the "ignore" procedure after "getline" procedure.

For numbers and ONE characters: use cin >> variablename

For words(a string with XX characters) use cin.get or cin.getline

PS! You have to read the first line of the file before you start the while loop, like this:


cin.get(name, 30); cin.ignore();  //this goes only ONE time
while (!infile.eof()) {  //while the file is NOT empty
  cin.get(name2, 30);
  cin.ignore();
  cin.get(name3, 30);
  cin.ignore();
  cin.get(name, 30);  
  cin.ignore();
}


like this???? I would have done it........


Robert
0
 
nietodCommented:
>> If you want to read a word(more than one character),
>> you have to use the "cin.get"
Not true.   There are many things you can use, including operator >>

>> When you reading from a file, you can also
>> use the "getline" procedure.
All streams classes have the same I/O interface.  So you can use getline() from ALL streams.

>> Then you have to have ignore
>> procedure after.
have to?  You don't have to.  You use ignore() only if you want to ignore some of the data in the stream.  If you don't want to ignore some of that data, then you don't use ignore().  In most cases you would not want to ignore data.

>> For words(a string with XX characters)
>> use cin.get or cin.getline
By default operator >> will read whole words (delimited by whitespace) so operator >> is usually used to read words.  get() and getline() don't have this behavior so are less convenient for this.

>> You have to read the first line of the file
>> before you start the while loop,
No!  
0
 
rob_dragCommented:
Hello!!

yea...sorry you right.....but >> does not read a line with a SPACE between to words

getline and get works with any I/O interface...yea that's true, but I have always used get operator when reading from file or from the user.

end of every line/word you write, the \r is placed on the end. f.eks: you write "Jordan is the best basketball player ever", and C++ writes it "Jordan is the best basketball player ever\r".
And if you read more than one line/word you have to ignore the ENTER character. If not, ENTER(\r) is reading into your variables. every variables you have will be empty!!!!! So then you have to have the ignore procedure after get.........

PS! Maybe you don't have to use ignore if you're reading only words from a I/O. But when you're reading a line with many words you have to have the ignore procedure....


I think you would read the first line before you start reading the rest of the file. I'm quit sure that you must read line 1 before "Rest of the file". If not , the first line will not be read.


Robert
0
 
nietodCommented:
>> but >> does not read a line with
>> a SPACE between to words
By default it reads a word, not a line.  That's what I've been saying.  But I beleive wou can make it read multiple words at a time by clearing the skipws (skip white space) flag.

>> end of every line/word you write, the \r
>> is placed on the end
No.  You have to tell it do so so.

>> If not, ENTER(\r) is reading into your
>> variables. every variables you have will be
>> empty!!!!! So then you have to have the
>> ignore procedure after get.
No

>> when you're reading a line with many words
>> you have to have the ignore procedure.  
No.

>> I'm quit sure that you must read line 1
>> before "Rest of the file". If not , the first
>> line will not be read.
Well that's true....   but it has nothing to do with the while loop.
0
 
rob_dragCommented:
Hello!!!

well, i don't think we will be agree.

If you read something like this into one var.

PS!

line 1: "Bulls is the world champions" ---> cin.get(teaminfo, 80);
cin.ignore();
line 2: "Bulls has won 6 championsship" --->cin.get(teamwon, 80);
cin.ignore();

line 3: "Lakers is one of the best teams in the NBA" --->cin.get(teaminfo, 80);

line 4: "Lakers have non rings in the 90's" ---> cin.get(teamwon,80);
cin.ignore();

Here you have to have cin.ignore, if not send me a some lines that shows me how!!!!!!


I have made alot of programs that is reading data from files, and every time i have to have the ignore procedure after a get........well....i'm not an expert in C++ and you may are......



Robert
0
 
rob_dragCommented:
Hello!!!

well, i don't think we will be agree.

If you read something like this into one var.

PS!

line 1: "Bulls is the world champions" ---> cin.get(teaminfo, 80);
cin.ignore();
line 2: "Bulls has won 6 championsship" --->cin.get(teamwon, 80);
cin.ignore();

line 3: "Lakers is one of the best teams in the NBA" --->cin.get(teaminfo, 80);

line 4: "Lakers have non rings in the 90's" ---> cin.get(teamwon,80);
cin.ignore();

Here you have to have cin.ignore, if not send me a some lines that shows me how!!!!!!


I have made alot of programs that is reading data from files, and every time i have to have the ignore procedure after a get........well....i'm not an expert in C++ and you may are......



Robert
0
 
nietodCommented:
robert, you have logged in as an expert, not a client!

There are many ways to read that input without using ignore().  
I think it is best to use a string class and the global getline() functions, but if you want to read into a character array you can use the version of get() that allows you to specify a delimter, like

cin.get(CharArray,80,'\n');
0
 
rob_dragCommented:
It's more than 5-6 months ago last time I logged in here....so I just logged in and "took a look" around here and find this question to answer.........so i didn't know that i logged in as an expert!!!!

yea....there you have it...you can call the get prcodedure with 2 or 3 arguments.....




0
All Courses

From novice to tech pro — start learning today.