tags:

views:

758

answers:

5

I found a C++ source file which calculates expressions from a command line argument (argv[1]), however I now want to change it to read a file.

double Utvardering(char* s) {
srcPos = s;
searchToken();
return PlusMinus();
}

int main(int argc, char* argv[]) {
if (argc > 1) {
    FILE* fFile = fopen(argv[1], "r");
    double Value = Utvardering(fopen(argv[1], "r"));
    cout << Value << endl;
}else{
    cout << "Usage: " << argv[0] << " FILE" << endl;
}
cin.get();
return 0;
}

However the Utvardering function requires a char* parameter. How can I convert the data read from a file, fopen to a char*?

+1  A: 

I have no idea what "Utvardering" expects, or how it's using the information.

There are two possibilities -

1) Utvardering may be defined using char*, but expecting a FILE* (in effect, treating char* like void*). I've seen this before, even though it's pretty awful practice. In that case, just cast fFile to char* and pass it in.

2) Utvardering may be expecting a null terminated string (char*) as input. If you're using fopen like this, you can use fread to read the file contents into a buffer (char[]), and pass it to your function that takes a char*.

Reed Copsey
+6  A: 

Use the fread() function to read data from the FILE* into a buffer. Send that buffer into Utvardering().

Magnus Skog
remember to fclose() it afterwards!
sean riley
A: 

It looks like you need to write code to read the file into a character array and pass that to Utvardering.

Just passing the return value of fopen will cause the address of the opaque data structure pointed to by that pointer to be passed to Utvardering. Utvardering will happily treat those bytes as character data when they are not. Not good.

Sinan Ünür
+4  A: 

The function fopen just opens a file. To get a string from there, you need to read the file. There are different ways to to this. If you know the max size of your string in advance, this would do:

const int MAX_SIZE = 1024;
char buf[MAX_SIZE];
if (!fgets(buf, MAX_SIZE, fFile) {
  cerr << "Read error";
  exit(1);
}
double Value = Utvardering(buf);

Note: this method is typical for C, not for C++. If you want more idiomatic C++ code, you can use something like this (instead of FILE and fopen):

ifstream in;    
in.open(argv[1]); 
if (!in) { /* report an error */ }
string str;
in >> str;
Igor Krivokon
Where is MAX_SIZE declared?
James Brooks
You can declare MAX_SIZE as a constant either in the body of the function, or outside, as a global constant. Usually the less clutter in the global namespace, the better, so just put it inside the main function. I've updated the example, see above.
Igor Krivokon
The first example doesn't set it to char *, any ideas?
James Brooks
Could you explain, what do you mean by "doesn't set it to char*"? For debugging, you can add this line before call to the Utvardering: printf("%s\n", buf); to make sure the buf contains the same string as your file.
Igor Krivokon
I get this error:123 C:\Dev-Cpp\Projects\TestLang\test1.cpp cannot convert `char*' to `FILE*' for argument `3' to `char* fgets(char*, int, FILE*)'
James Brooks
The 3rd argument to fgets should be fFile. Do you declare and initialize it as FILE* fFile = fopen(argv[1], "r"); ?
Igor Krivokon
Brilliant! Thanks Igor!!!
James Brooks
A: 

Good example of reading data from a file here: http://www.cplusplus.com/reference/clibrary/cstdio/fread/ then pass the buffer to your function

Patrick