|
Hi i'm new to c++
and i'm trying to read from a file character by character and output the result on the console.
This is my code:::
#include
#include
int in_tags(FILE *pFile)
{
char t;
cout<<"in tags";
t=getc(pFile);
cout<<t;
while (t=getc(pFile)!='>')
{
cout<<t;
}
return 0;
}
int main ()
{
FILE * pFile;
char c;
bool tag;
pFile = fopen("/forum/example.txt","r");
if (pFile==NULL) perror ("Error opening file");
else
{
do {
c = getc (pFile);
cout<<c;
if(c=='<')
{
tag=in_tags(pFile);
}
} while (c != EOF);
fclose (pFile);
}
return 0;
}
THE OUTPUT::
okto continue
The FILE::
okok ok okok ok ok ok
The output that i get from the main program is ok
but when are coming from the function within the while loop
the caracters are lost and i get symbols.
I hope i'm clear enough but i don't think so but in case you know how to help me pls reply as this is for a project that i'm doing for college
thanks in advance!
|
|
|
Your in_tag function isn't returning a value.
And if you want it to return characters then you'd have to change it from "int int_tag" to something that would support a returned character/string value.
|
|
|
This is the in_tags that you want:
int in_tags(FILE *pFile)
{
char t;
while ((t=getc(pFile))!= '>')
{
cout<<t;
}
cout << t;
return 0;
}
The main problem that you had was the while loop seemed to better confused with operator precence for = and !=
If you are just getting started in programming, some advice....
1. avoid do-while loops. Use while() loops instead. They are better for readability of the code.
2. Whitespace is your friend.
3. If you have any doubts about operator precence (order in which expressions are evaluated) use brackets. They are also your friend.
Good luck.
|
|
|
|
|
|
|
// |