The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
Dev Shed Forums
> Programming Languages
> C Programming
Error in getline() Argument
Discuss Error in getline() Argument in the C Programming forum on Dev Shed. Error in getline() Argument C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Dev Shed Forums Sponsor:
July 31st, 2009, 05:54 PM
PixHost.tk Developer
Join Date: Jul 2009
Location: Brazil
Posts: 58
Time spent in forums: 1 Day 25 m 29 sec
Reputation Power: 0
Error in getline() Argument
Hello,
When i try to compile this simple example for learning i have some errors while compiling:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
char temp[100];
ifstream file("test.txt");
string linha = getline(temp, 100, file);
if(linha == "teste")
{
cout << "Hello, World";
}
}
And here is the log of g++:
Code:
ubuntu@ubuntu-laptop:~/Desktop$ g++ -o test test.cpp
test.cpp: In function ‘int main()’:
test.cpp:10: error: cannot convert ‘char*’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
ubuntu@ubuntu-laptop:~/Desktop$
What is wrong?
Thanks,
Nathan Paulino Campos
July 31st, 2009, 06:03 PM
Banned ;)
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Your problem is around where you're using getline(). You should never have to use a char buf, when you have C++ and a string object.
Simply do this instead.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
ifstream file("test.txt");
string linha;
getline(file, linha);
if(linha == "teste")
{
cout << "Hello, World";
}
return 0;
}
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
July 31st, 2009, 06:24 PM
PixHost.tk Developer
Join Date: Jul 2009
Location: Brazil
Posts: 58
Time spent in forums: 1 Day 25 m 29 sec
Reputation Power: 0
Thanks very much!!!!!!!!!!!!!
July 31st, 2009, 06:31 PM
PixHost.tk Developer
Join Date: Jul 2009
Location: Brazil
Posts: 58
Time spent in forums: 1 Day 25 m 29 sec
Reputation Power: 0
But how i can do if my file is like this:
And i want to show the two, the one like in your post and the second like this:
Code:
if(linha == "arquivo")
{
cout << "Testing...\n";
}
How i can do this??????????
Last edited by nathanpc : July 31st, 2009 at 06:34 PM .
Reason: put the code
July 31st, 2009, 07:18 PM
If you want to get two lines I suppose you could use "getline" twice. Stuff like that.
July 31st, 2009, 07:24 PM
PixHost.tk Developer
Join Date: Jul 2009
Location: Brazil
Posts: 58
Time spent in forums: 1 Day 25 m 29 sec
Reputation Power: 0
Thanks, but i solved thid problem: http://www.dreamincode.net/forums/showtopic117882.htm
Comments on this post
sizablegrin
disagrees: Great. Nothing like making us waste and duplicate effort without telling us.
holodoc
agrees: Balance... You found help elsewhere and even gave the link to the solution... Its unfair to derep
you for that.
Developer Shed Advertisers and Affiliates
Thread Tools
Search this Thread
Display Modes
Rate This Thread
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off