getting a string from console and put it in a file

This is a discussion on getting a string from console and put it in a file within the C Programming forums, part of the General Programming Boards category; Code: int main(){ FILE *inputfile,*outputfile; char temp[1000]; fgets(temp,1000,stdin); //get one-liner string from console and store it to temp inputfile=tmpfile(); //open ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    3

    getting a string from console and put it in a file

    Code:
    int main(){
    	FILE *inputfile,*outputfile;
    	char temp[1000];
    	
    	fgets(temp,1000,stdin); //get one-liner string from console and store it to temp
    	inputfile=tmpfile(); //open a temporary file
    	fputs(temp,inputfile); //copy the string in temp to fp
    	fflush(inputfile); //flush to solve stream buffering
    	outputfile=fopen("output.txt", "wt");
    	
    	yyin=input;
    	yyout=output;
    	yylex();
    	floseall;
    }
    Actually, I'm working with LEX right now. I posted above the C language part of my program. Just don't bother with the yyin, yyout and yylex(), they're part of the LEX. So here is my problem:

    I need to get a one-liner stdin input (meaning, the input will already be entered once you press enter). Then I need to copy that input to a file. Then that file is what I need for yyin, the input to yylex().

    Here's how I approached the problem:


    fgets(temp,1000,stdin);
    First, I used fgets(). During some "experiments" i had done earlier, I confirmed that it was able to get all those inputed including the newline character.

    inputfile=tmpfile();
    Then I used tmpfile() to open a temporary file (well, we are not allowed to create a file on the physical space)

    fputs(temp,input);
    Then I used fputs. It seems this is where the problem started. For some reason, I can't have the inputfile contain what I inputed in the console. Otherwise, then yylex() would have been able to process the yyin.

    I even used fflush. I've experimented with it a while ago, and the fflush there is working fine. It is not returning any error values.

    Yeah this is LEX. But I'm only asking about the C language part. I'm just trying to have inputfile contain the string inputed in the console. Also, the inputfile should not create any file in the physical space. Anybody?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Perhaps reset the position indicator back to the beginning of the file?

    fseek( inputfile, 0, SEEK_SET );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    3
    ^Thanks very much. That worked. This forum is really awesome. It's my first time using fseek so I don't know it. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do i put a graphics - console application
    By hero_bash in forum C++ Programming
    Replies: 10
    Last Post: 06-12-2006, 05:03 PM
  2. How do I make a string buffer?
    By pliang in forum C++ Programming
    Replies: 6
    Last Post: 04-11-2005, 07:19 AM
  3. How to put a Char into the Input buffer of a console?
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2003, 09:05 AM
  4. once mor file operation
    By stormbringer in forum C Programming
    Replies: 4
    Last Post: 12-20-2002, 06:31 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21