LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2006, 02:06 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
error: invalid conversion from `void*' to `char**', where should I start to look for


I'm doing my best to create functions for my program to read and write characters from a text file... just one small problem... on line 45 of the following code shown below, it cannot convert void* to char*. Here is the actual output with the g++ command:

Code:
[georig1\ 23:52:25\ george\ ~/cfiles/George Lair/050606]$ g++ TextINOUT.cpp -Wall -Wextra -o TextINOUT3
TextINOUT.cpp: In function `std::string* ReadOut()':
TextINOUT.cpp:45: error: invalid conversion from `void*' to `char**'
TextINOUT.cpp:45: error: cannot convert `std::string*' to `size_t*' for argument `2' to `__ssize_t getline(char**, size_t*, FILE*)'
[georig1\ 00:00:20\ george\ ~/cfiles/George Lair/050606]$
... here is the actual code that I made:

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
using namespace std;

string * ReadOut(void); // This operates in conjunction with ReadFilesLines()
void WrightIn(string STRING); // This operates alone.
void ReadFileLines(void); // This operates in conjuction with ReadOut()

int main () {

	string UserInput; // Data of UserInput goes to WrightIn()
	string line;

	cout << "\n\tWright something you want to put into a file, and I will display what you wrote.\n\n";
	cout << "> ";
	getline(cin,UserInput); cout << UserInput << " : Just to let you know that is what you wrote.\n"; // Input data into UserInput

	WrightIn(UserInput); // Writes whatever the user inputed into UserInput into the example.txt file
	ReadFileLines(); // Reads whatever the return value of ReadOut() is.

  return 0;
}

void WrightIn(string STRING)
{
  fstream infile ("example.txt");
  if (infile.is_open())
  {
    infile << STRING;
    infile.close();
  }
  else { cout << "Unable to open file"; }
}

string * ReadOut(void)
{
	fstream outfile ("example.txt");
	string * MESSAGE = new string;	
  if (outfile.is_open())
  {
    while (!outfile.eof())
    {
        getline(outfile,MESSAGE);
	cout << "\nJust making sure this testPro is reaching this function.\n";
    }
    outfile.close();
  }
  else { cout << "Unable to open file"; }

	return MESSAGE;
}

void ReadFileLines()
{
	string * READ = ReadOut();
	cout << "ReadFilesLines should be outputing the string READ from ReadOuts() return!\n\n"; cout << READ << endl;
	delete READ; READ = NULL;
}
What exactly should I do to declare string * MESSAGE with whatever characters found in "example.txt"? If you find that it is cheating to give me the full answer, I have no problem with a push in the right direction, whatever that push maybe. Thank you in advance.
 
Old 05-19-2006, 03:32 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Need to read over the man page for getline.

You're missing an entire argument

EDIT:
Since you're writing C++, you probably don' mean to use the regular getline() function. I assume you intended to use the getline member of an istream class. In that case, you need to invoke it as such--in other words, invoking it as a reference to an object (which I assume you intend "outfile").

Also, it's a little confusing, because getline is associated with an input stream--not an output stream. The variable names are a bit out-of-whack because you refer to the fstream as an "outfile" when you want to read from it.

EDIT-EDIT:
According to my reference book, the istream getline() method's prototype is:
istream &getline(char *buf, streamsize num);

Places num-1 characters into the buffer pointed to by buf.

Last edited by Dark_Helmet; 05-19-2006 at 03:44 AM.
 
Old 05-19-2006, 11:41 PM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889

Original Poster
Blog Entries: 1

Rep: Reputation: 30
I don't understand, the following works (found on cplusplus.com tutorials):

Code:
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
... but I read this when I look at the c++ reference to istream::getline():

Quote:
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

Get a line from stream.
Extracts characters from the stream and stores them into successive locations in the array pointed by s.
Characters are extracted until either (n - 1) characters have been extracted, the delimiter (parameter delim or '\n' if not specified) is found, or if the end of file or any error occurs in the input sequence.
If the delimiter is found it is extracted but not not stored. Use get if you don't want this character to be extracted.
An ending null character is automatically appended after the data stored in s.
Code:
TextINOUT.cpp:47: error: invalid conversion from `void*' to `size_t*'
is still what I get. But I don't get why I don't recieve the error from cplusplus's resource on getline and input output functions. It's reading from a source and storing it into a destination(getline (myfile/*source*/,line/*destination*/);, not extracting the source up until a certain amount of characters have been reached.

I have read man fopen, and I have tried to use it but with failure. Seems as though this is a c style way of reading from and writing a file.(stdio.h, stdlib.h, which in c++ is cstdio cstdlib) Here is the code provided with the man page of getline:

Code:
 
       #define _GNU_SOURCE
       #include <stdio.h>
       #include <stdlib.h>

       int main(void)
       {
            FILE * fp;
            char * line = NULL;
            size_t len = 0;
            ssize_t read;
            fp = fopen("/etc/motd", "r");
            if (fp == NULL)
                 exit(EXIT_FAILURE);
            while ((read = getline(&line, &len, fp)) != -1) {
                 printf("Retrieved line of length %zu :\n", read);
                 printf("%s", line);
            }
            if (line)
                 free(line);
            return EXIT_SUCCESS;
       }
When you say invoke the istream member function getline, to me it means to call upon its object name, such as the following:
Code:
 { MyClass ObjClass; ObjClass.getline(bleh,bleh,bleh);}
correct?

--PS--
If I misread something please point it out to me. Or correct me on something that I believe may be true... for I'm certain it's not. So I guess I don't believe it's true! :>

Last edited by RHLinuxGUY; 05-19-2006 at 11:48 PM.
 
Old 05-19-2006, 11:50 PM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by RHLinuxGUY
I'm doing my best to create functions for my program to read and write characters from a text file... just one small problem... on line 45 of the following code shown below, it cannot convert void* to char*. Here is the actual output with the g++ command:

Code:
[georig1\ 23:52:25\ george\ ~/cfiles/George Lair/050606]$ g++ TextINOUT.cpp -Wall -Wextra -o TextINOUT3
TextINOUT.cpp: In function `std::string* ReadOut()':
TextINOUT.cpp:45: error: invalid conversion from `void*' to `char**'
TextINOUT.cpp:45: error: cannot convert `std::string*' to `size_t*' for argument `2' to `__ssize_t getline(char**, size_t*, FILE*)'
[georig1\ 00:00:20\ george\ ~/cfiles/George Lair/050606]$
... here is the actual code that I made:

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
using namespace std;

string * ReadOut(void); // This operates in conjunction with ReadFilesLines()
void WrightIn(string STRING); // This operates alone.
void ReadFileLines(void); // This operates in conjuction with ReadOut()

int main () {

	string UserInput; // Data of UserInput goes to WrightIn()
	string line;

	cout << "\n\tWright something you want to put into a file, and I will display what you wrote.\n\n";
	cout << "> ";
	getline(cin,UserInput); cout << UserInput << " : Just to let you know that is what you wrote.\n"; // Input data into UserInput

	WrightIn(UserInput); // Writes whatever the user inputed into UserInput into the example.txt file
	ReadFileLines(); // Reads whatever the return value of ReadOut() is.

  return 0;
}

void WrightIn(string STRING)
{
  fstream infile ("example.txt");
  if (infile.is_open())
  {
    infile << STRING;
    infile.close();
  }
  else { cout << "Unable to open file"; }
}

string * ReadOut(void)
{
	fstream outfile ("example.txt");
	string * MESSAGE = new string;	
  if (outfile.is_open())
  {
    while (!outfile.eof())
    {
        getline(outfile,MESSAGE);
	cout << "\nJust making sure this testPro is reaching this function.\n";
    }
    outfile.close();
  }
  else { cout << "Unable to open file"; }

	return MESSAGE;
}

void ReadFileLines()
{
	string * READ = ReadOut();
	cout << "ReadFilesLines should be outputing the string READ from ReadOuts() return!\n\n"; cout << READ << endl;
	delete READ; READ = NULL;
}
What exactly should I do to declare string * MESSAGE with whatever characters found in "example.txt"? If you find that it is cheating to give me the full answer, I have no problem with a push in the right direction, whatever that push maybe. Thank you in advance.
getline() takes a string as its second argument. MESSAGE is a pointer to a string. That's why it does not work.
 
Old 05-20-2006, 08:53 PM   #5
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889

Original Poster
Blog Entries: 1

Rep: Reputation: 30
I fixed the problem with the help shown above. I kept thinking that I had to point MESSAGE to another object when I didn't need, thats how I understand the situation at least. Every instance that MESSAGE, or the variable that is made from the heap, is declared for input or output (excluding return), I needed to prefix MESSAGE with an asterisk. So the following works:

Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
using namespace std;

string * ReadOut(void); // This operates in conjunction with ReadFilesLines()
void WrightIn(string STRING); // This operates alone.
void ReadFileLines(void); // This operates in conjuction with ReadOut()

int main () {

	string UserInput; // Data of UserInput goes to WrightIn()
	string line;

	cout << "\n\tWright something you want to put into a file, and I will display what you wrote.\n\n";
	cout << "> ";
	getline(cin,UserInput); cout << UserInput << " : Just to let you know that is what you wrote.\n"; // Input data into UserInput

	WrightIn(UserInput); // Writes whatever the user inputed into UserInput into the example.txt file
	ReadFileLines(); // Reads whatever the return value of ReadOut() is.

  return 0;
}

void WrightIn(string STRING)
{
  fstream infile ("example.txt");
  if (infile.is_open())
  {
    infile << STRING;
    infile.close();
  }
  else { cout << "Unable to open file"; }
}

string * ReadOut(void)
{
	fstream outfile ("example.txt");
	string * MESSAGE = new string;
	
  if (outfile.is_open())
  {
    while (!outfile.eof())
    {
        getline(outfile,*MESSAGE);
	cout << "\nJust making sure this testPro is reaching this function:1:.\n";
	cout << *MESSAGE << endl;
    }
    outfile.close();
  }
  else { cout << "Unable to open file"; }

	return MESSAGE;
}

void ReadFileLines()
{
	string * READ = ReadOut();
	cout << "ReadFilesLines should be outputing the string READ from ReadOuts() return!\n\n"; cout << *READ << endl;
	delete READ; READ = NULL;
}

Last edited by RHLinuxGUY; 05-20-2006 at 08:54 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
Conversion from char[100] to char* ? zahadumy Programming 2 12-11-2005 09:04 PM
Help with Conversion from Sting to Char Diederick Programming 2 11-30-2005 08:00 AM
C programming - Char to Int Conversion ? indian Programming 4 09-30-2005 08:00 AM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration