Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STDIN ISSUES

Status
Not open for further replies.

smythe

Programmer
May 30, 2001
49
US
I'm using a c++ program (not MFC) to read input in via stdin then write back out using stdout.

type filename.txt | myprogram

stdout works fine, but stdin is giving me strange behavior.

1) I keep getting this error message
"The process tried to write to a non-existent pipe."

I have no idea what this means or what needs to be done to resolve it.

2) The first time I run my program, it reads in about half of my file in but then stops, after that it doesn't read anything in... if I recompile the program, it reads half a file again, and the cycle continues... now that's strange

Has anyone seen this before? Any ideas on what steps to resolve this?

Here's how I'm receiving input:

// file pointer to read file
FILE * pFile;

// receive input via stdin
pFile = stdin;

// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);

// seek back to beginning of file
rewind (pFile);

// allocate memory to contain the whole file.
buffer = (char*) malloc (lSize);

// read the file buffer in
fread( buffer,1,lSize,pFile);

The data should all be in buffer, but it's not, only half of it on the first try, then it crashes!
 
> fseek (pFile , 0 , SEEK_END);
seeking on stdin is probably not a good idea

Try
Code:
if ( fseek (pFile , 0 , SEEK_END) != 0 ) {
    perror( "oops" );
    exit( 1 );
}
My manual page has
EBADF The stream specified is not a seekable stream.

Since you don't know in advance how much input there is, you need an expanding data structure like a linked list.

> to read input in via stdin then write back out using stdout.
Such programs normally deal with a 'char at a time' or a 'line at a time'. What are you doing which means you have to read the whole file before writing anything back out?
 
"Since you don't know in advance how much input there is, you need an expanding data structure like a linked list."

Thanks, I figured this out working with a fellow programmer.
How would you suggest I accomplish this? Read one char at a time and push to the list/vector then writing to the buffer when completed?

"Such programs normally deal with a 'char at a time' or a 'line at a time'. What are you doing which means you have to read the whole file before writing anything back out?"

Normally I would read in the file line by line, but I am parsing a C header file containing structs to extract certain information... since I'm not looking for particular information on each line (just looking for certain states based on the data), I figured this was the easiest way to do it. Changing the algorithm now will be more work than perhaps using your linked list suggestion.

 
I guess the solution we've decided on is to read the stdin in line by line using fgets(), then write the data out to a temporary file then read in from that temp file.. then delete the file. I tried reading in char by char using fgetc(), but this ignores whitespace and will not give me an accurate input size to allocate for a buffer.

fgets() does not require the size of the buffer, and this way we do not need to change the entire algorithm (which was intended to work with file input from the beginning).

Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top