codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  function getc problem - newbie  antonaras at 09:30 on Wednesday, March 08, 2006
 

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!


  Re: function getc problem - newbie  80Degrees at 18:54 on Thursday, March 30, 2006
 

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.

  Re: function getc problem - newbie  INeedHelp2 at 00:27 on Friday, March 31, 2006
 

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.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  perl script help needed
•  onChange issue
•  perl remote execution
•  Chat application
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime
•  help me


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007