FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » C++ Help Meta Thread

   
Author Topic: C++ Help Meta Thread
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
Okay using puts/gets how do I read lines from a file?

Example how would I read a file line by lione and then print each line as it comes out?

IP: Logged | Report this post to a Moderator
ricree101
Member
Member # 7749

 - posted      Profile for ricree101   Email ricree101         Edit/Delete Post 
link

Here's a reference page for stdio.h. Specifically, look up fgets and puts. If you have any more specific questions, feel free to ask them, but this page should give you a general idea.

Posts: 2437 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
Nighthawk
Member
Member # 4176

 - posted      Profile for Nighthawk   Email Nighthawk         Edit/Delete Post 
"gets" is arguably dangerous because it doesn't do bounds checking and is vulnerable to stack overflows.

I generally created my own:

code:
char *my_gets( FILE *fp, char *szStr, int iLength )
{
memset( szStr, 0, iLength );
bool bEOL = false;
while ( !feof( fp ) && (strlen(szStr) < iLength - 1) )
{
int cCh = fgetc( fp );
if ( cCh == '\n' )
{
bEOL = true;
break;
}
else
szStr[strlen(szStr)] = cCh;
}
// If buffer is shorter than line, read rest of line and ignore it
if ( !bEOL )
{
while ( !feof( fp ) )
{
if ( fgetc( fp ) == '\n' )
break;
}
}
return szStr;
}

*EDIT*: The above has not actually been compiled because it's off the cuff, but it *looks* right.
Posts: 3486 | Registered: Sep 2002  |  IP: Logged | Report this post to a Moderator
ricree101
Member
Member # 7749

 - posted      Profile for ricree101   Email ricree101         Edit/Delete Post 
gets is dangerous, but fgets has a parameter where you can put the buffer size, so there is much less danger.
Posts: 2437 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
Nighthawk
Member
Member # 4176

 - posted      Profile for Nighthawk   Email Nighthawk         Edit/Delete Post 
But what I hate about "fgets" is that it reads the string including the carriage return. Also, if I remember correctly, if you read using a buffer less than the actual string length, the rest of the string still sits waiting to be read, unlike the above which will ignore the remainder of the line.
Posts: 3486 | Registered: Sep 2002  |  IP: Logged | Report this post to a Moderator
ricree101
Member
Member # 7749

 - posted      Profile for ricree101   Email ricree101         Edit/Delete Post 
Really? I hadn't realized that. I can see why that'd be a pain.
Posts: 2437 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
Dav
Member
Member # 8217

 - posted      Profile for Dav           Edit/Delete Post 
I've always thought of that as a handy feature of fgets, actually. But I guess it depends on what you're using it for.
Posts: 120 | Registered: Jun 2005  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
ok one thing to know the function another to know how to implement it, for somehting as simple as just reading and outputting lines of a file how would I do it? My memory shorted out on my for when my prof was explaioning this.
IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
I think you are misusing the terminology, there. I would be extremely surprised if your assignment is to 'implement' a get/put function; that's low-level stuff for compiler and API writers. What you are trying to do is use the function, and already somebody has very kindly linked you the documentation. Go read it.
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
it makes no sense to me though.

code:
char* fgets(char* s, int n, FILE* stream);


Copies characters from (input) stream stream to
s, stopping when n-1 characters copied, newline
copied, end-of-file reached or error occurs. If no
error, s is NUL-terminated. Returns NULL on
end-of-file or error, s otherwise.

like do I
just do fgets(filename); and it will read it will
read the first line?

wait is it say fgets( streamvar, MAX, filename);?

IP: Logged | Report this post to a Moderator
ricree101
Member
Member # 7749

 - posted      Profile for ricree101   Email ricree101         Edit/Delete Post 
a FILE* is returned by fopen. s is the buffer to write the data into (which should be created with something like char buffer[n] where n is big enough to store your data. The int n parameter is the size of your buffer, so that there isn't an overflow.
Posts: 2437 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
okays got it to work,magically thing called "google" on fgets, turns out I needed to use my variable "tline", make a point called pFile use fopen on "test.exe" and stuff blah easier to paste it.

code:
int main()
{
FILE * pFile;
char string [100];

pFile = fopen ("myfile.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
fgets (string , 100 , pFile);
puts (string);
fclose (pFile);
}
return 0;
}


IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
*sigh* now to figure out how to get it to get each line not just first on, me thinks a loop.
IP: Logged | Report this post to a Moderator
Will B
Member
Member # 7931

 - posted      Profile for Will B   Email Will B         Edit/Delete Post 
cin.getline (myLine, MAXLINELENGTH);

cout arrow-arrow myLine arrow-arrow endl;

(Hatrack's software doesn't like the usual istream operator it seems)

...and, yes, a loop. Probably one starting with "while (cin)".

Posts: 1877 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Incidentally, you should get used to thinking in loops. Almost everything is a loop. Loops are what separate markup languages like HTML from real Turing-complete languages with hair on their chests. You should also look up Turing completeness, it's a fascinating concept. Then look up brain****, and ponder the fact that it is provably Turing complete.
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
the lines from a fie and I got that to work, basically

code:
pfile = fopen("test.txt");

loop
{

fgets( tline, max, pfile);
puts(tline);
}

fclose;

problem, at the last line it keeps on putting the last line and not exiting the rpgm, i have it so that if strlen(tline) > 0, loop. So if it hits an empty line it shuld exit but it aint.
IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
ok for some reason a blank line counts as being 1 char/number so strlen always reutrned 1 if it was a blank line.
IP: Logged | Report this post to a Moderator
HollowEarth
Member
Member # 2586

 - posted      Profile for HollowEarth   Email HollowEarth         Edit/Delete Post 
You don't want to use a strlen call to delimit the loop. Look at the return value of fgets.

code:
while(fgets(...))

Look here they tell you what kinds of things fgets can return. (Actually, judging from your example above it looks like you've already been there.)

Edit: read the docs more carefully. (or even this thread). fgets considers a new line a valid character so a blank line should have a length of one. (the '\n' char)

Posts: 1621 | Registered: Oct 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
ah ha.
IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
okay so I use fgetc o read in characters from a file.

quote:


doing and wrong-doing , there is a field. Furthermore, he asserted-

now part of the code is that if I hit a End of Word Event (EOWvnt) example ", .;-" then replace the first instance of it with a "#" an leave he rest alone.


quote:


doing#and#wrong#doing#, there#is#a#field@ Furthermore# he#asserted#

and of course same thing for EOS events, "!?."

with a @.

however I am supposed to keep hypthenation in mind so:

if you have "wrong-doing" leave the hyphen alone.

Currently my code works under the assumtion of as soon as I hit a EOWvnt, cout << "#"; instead of the char.

and change the flag so that all next encounters of EOW chars are ignored and outputted as normal.

But how do I get it so that it ignores the first instance of "-" if there is a word next?

since it reads only one char at a time by the time I get to check if the next char is either a EOWvnt or a normal character the first hyphen would alrdy be either a '-' or a '#'.

Does C++ have a backspace command?

IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
code:

code:
#include <iostream>
#include <stdio.h> // for gets(), puts()
#include "string.h"

using namespace std;

const int MAX = 100; //size of arrayqq

int pass, size, t, j, x[MAX]; //
char eoschrs[MAX] = ".?!";
char eowchrs[MAX] = " ,:;-";

void EOWEvent( char );
void EOSEvent( char );


int main()
{
int flag, flag2, quote;
flag = 1;
flag2 = 1;
char c;

FILE *pFile;

quote = 0;


pFile = fopen( "test.txt", "r" );
do
{
c = fgetc (pFile);

if ( c == '"' )
{
quote++;
if ( quote >= 2 )
{
quote = 0;
}
}

if ( ( c == '.' || c == '?' || c == '!' ) && flag == 1 && quote != 1 )
{
EOSEvent( c );
flag = 0;
flag2 = 0;

}
else if ( c == '-' )
{

//stuff
}
else if ( (c == ' ' || c == ',' || c == ':' || c == ';' || c == '\n') && flag2 != 0 )
{
EOWEvent( c );
flag = 1;
flag2 = 0;
}
else
{
cout << c;
flag = 1;
flag2 = 1;

if ( c == '.' || c == '?' || c == '!' )
{
flag = 0;
flag2 = 0;

}

if ( c == ' ' || c == ',' || c == ':' || c == ';' || c == '-' )
{
flag2 = 0;
}
}
}
while (c != EOF);
fclose( pFile );
cout << endl << endl;

system("pause");


return 0;

}


void EOWEvent( char EOWvnt )
{
cout << "#";

}

void EOSEvent( char EOSvnt )
{
cout << "@";


}


IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Since it's a special case, treat it special. There is no rule that says you can only use fgetc at the start of the loop.
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
ack why wont this work,

code:
#include <iostream>

using namespace std;

void main()
{

cout << "hello world" << endl;

}

im getting unresolved external, i hate errors that arent descriptive.


I'm trying to make a test program for the function chfind but i notice that all my errors fall back to this for some reason if i comment everything out.

IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Sounds like your compiler can't find the implementation (not the header file, but the actual binary) of iostream - at least, if you're really showing all your code, that's all I can think of. Check your path settings and whatnot.
Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
okay i found the problem for sme reason when I created a new project i mustve chosen a wrong setting, i created a new project and works fine, I'm doing some pretty wierd things here.

Basically im thinking of using a function "chfind" provided by the teacher to look for what the next char is to see if its a letter or a hyphen or not.

In the end im confident it will work im only worried if im kinda deviating from my teachers specifications, as is I cant understand what he really wants chfind to do, all I know is I pass it the char I'm looking for and the target string and it returns a number, -1 or th position of n, n being the position of the char im looking for in the target string.

IP: Logged | Report this post to a Moderator
   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2