Click to See Complete Forum and Search --> : parse error in g++
blue_gene
01-13-2004, 02:28 PM
hi i am writing c /c ++ program in linux..... i am getting parse error.... what does this parse error means????
i guess this is something like syntax error..is it true????
say i am getting>>>> parse error at line 24..
but when i go to line 24 i find there is no syntax error...
in the neighbourhood of 24 line also there is no syntax error...
so my question is what is this parse error and how to remove this???
thanks
#include<stdio.h>
3 #include<vector>
4
5 int main()
6
7 {
8 vector<char> buff;
9 FILE *infile ;
10
11 char var;
12 int i = 0;
13 infile = fopen("data.txt","r");
14
15 while(!feof(infile))
16
17 {
18 fscanf(infile,"%c",&var);
19 buff.push_back(var);
20
21 }
22
23
24 for(int k=0;k<buff.sizeof();k++)
25
26 { printf("%c",buff[k]); }
27
28
29 }
30
31
32
33
34
35
error
-------
x.cpp: In function `int main ()':
x.cpp:24: parse error before `sizeof'
where i have done mistake?????
mwinterberg
01-13-2004, 02:48 PM
vector doesn't have a sizeof() member function, use size() instead.
-Michael
Citadel
01-13-2004, 02:56 PM
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int main() {
vector<char> buff;
char var;
int i = 0;
ifstream infile("data.txt");
//error check
if (!infile) {
string errMsg("unable to open file: ");
exit(1);
}
while(infile >> var ) {
buff.push_back(var);
}
for(int k = 0; k != buff.size() ; k++ ){
cout << buff[k];
}
}
Or use the STL idiom of iterators. Don't mix C and C++.
vector<char>::iterator iter = buff.begin();
while ( iter != buff.end() )
cout << *iter;
++iter;
}
blue_gene
01-13-2004, 04:20 PM
thanks ....its working now...i usued size()
hi , citadel
is your code running ??? i simply copied and pasted in vi editor and compiled in g++
but i am getting error >>> it s saying string undeclared..
anyway, i need to know
while(infile >> var )
how this loop will terminate??? its basically pouring data into var...so at last it will pour EOF into var...
....so one time var will get EOF .... does EOF behave as boolean false???
otherwise how while loop will terminate???
is there any other way to check this same condition
i would have satisfied if i could write
while(infile != EOF)
{ // stuffs }
is this legal in C++ ?
Citadel
01-13-2004, 04:32 PM
It compiled and ran on my system but try adding:
#include<string>
or else just remove the error message! I didn't even output it:
cout << errMsg;
If you can't see that, than you don't know c++. You should stick with plain C.
while (infile >> var) will terminate at the end of the file. It's standard c++. You should not mix C and C++. They are different languages, even though there is a high degree of compatibility. Get a book on C++ like C++ Primer 3rd edition.
Citadel
01-13-2004, 04:42 PM
If you want to mix the code use the c++ implementation such as <cstdio> instead of <stdio.h> because in c++ the libraries are contained in a namespace.
#include<cstdio>
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
using namespace std;
int main() {
vector<char> buff;
char var;
int i = 0;
ifstream infile("data.txt");
//error check
if (!infile) {
string errMsg("unable to open file: data.txt");
cout << errMsg;
exit(1);
}
while(infile >> var ) {
buff.push_back(var);
}
for(int k = 0; k != buff.size() ; k++ ){
cout << buff[k];
}
printf("C function used here.\n");
}
Citadel
01-13-2004, 04:49 PM
#include<cstdio>
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
using std::cout;
using std::string;
using std::vector;
using std::ifstream;
int main() {
...
or
#include<cstdio>
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
int main() {
std::vector<char> buff;
char var;
int i = 0;
std::ifstream infile("data.txt");
//error check
if (!infile) {
std::string errMsg("unable to open file: data.txt");
std::cout << errMsg;
exit(1);
}
while(infile >> var ) {
buff.push_back(var);
}
for(int k = 0; k != buff.size() ; k++ ){
std::cout << buff[k];
}
printf("C function used here.\n");
}
PolteRGeisT
01-13-2004, 04:54 PM
sizeof() is a reserve word, you can't use it as a member function.
blue_gene
01-13-2004, 05:10 PM
thanks......in fact i was writng C program.....i was interested to know vector bcoz i can store elements there without usuing dynamic memory.......
thats why i was trying to take advantage of vector in a .cpp extension program....although evryother thing is in C....i do not have that much background in C++.
thanks for the information
Citadel
01-13-2004, 05:11 PM
Here is a good old C version without any C++ types like vector:
#include<stdio.h>
int main() {
char buff[255];
FILE *fptr = fopen("data.txt","r");
while ( fgets( buff, sizeof(buff) / sizeof(buff[0]), fptr) != NULL ) {
printf("%s", buff );
}
fclose(fptr);
return 0;
}
Citadel
01-13-2004, 05:27 PM
Originally posted by blue_gene
thanks......in fact i was writng C program.....i was interested to know vector bcoz i can store elements there without usuing dynamic memory.......
thats why i was trying to take advantage of vector in a .cpp extension program....although evryother thing is in C....i do not have that much background in C++.
thanks for the information
The vector or any of the other containers in Standard C++ are useful but they are not part of C, so if you use it, you have to write a pure C++ program. Standard C++ is a finesse language, and it is hard to learn and although it is standardized and Linux tools are standard compliant, however we are basically living in a C environment here. So that's what everyone knows. It's very expensive to learn Standard C++, there are basically zero good quality tutorials or free online books. You have to pay a lot of money to learn it < www.accu.org >. See the book review section.
Citadel
01-13-2004, 05:28 PM
Originally posted by blue_gene
thanks......in fact i was writng C program.....i was interested to know vector bcoz i can store elements there without usuing dynamic memory.......
thats why i was trying to take advantage of vector in a .cpp extension program....although evryother thing is in C....i do not have that much background in C++.
thanks for the information
Also in Linux the extension should be .cc. The .cpp extension is Microsoft's influence, but the old Unix C++ follows the .cc convention.
Citadel
01-13-2004, 06:43 PM
Originally posted by blue_gene
hi , citadel
i would have satisfied if i could write
while(infile != EOF)
{ // stuffs }
is this legal in C++ ?
It's not legal in C++, but you could write:
while ( (infile >> var) != 0 )
There is a difference in C and C++ as to what zero and NULL mean.
In C, NULL represents a zero pointer, but in C++ a 0 (zero) is a zero pointer intead of NULL.
bwkaz
01-13-2004, 08:35 PM
The value 0 cast to any pointer type is NULL, in either C or C++.
if((int)NULL == 0) will always be true, and if(dynamic_cast<typeof(NULL)> (0) == NULL) will always also be true (if that's valid usage for typeof()... it might need to be typeid() but I don't remember for sure).