7 Replies - 8791 Views - Last Post: 30 January 2012 - 05:17 PM Rate Topic: -----

#1 praditmodi   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 30-November 11

Write standard input to file

Posted 30 January 2012 - 01:56 PM

#include<stdio.h>
void write_stdin();
main()
{
write_stdin();
}

void write_stdin()
{
char input[256];
FILE *p1;
p1=fopen("ofile.txt","w");

while(gets(input))
{

p1.write(input);
}
fclose(p1);
}



I want to write to a file the input received on the standard input / command line on execution this is the error i get :
myprog1.cpp:17: error: request for member âwriteâ in âp1â, which is of non-class type âFILE*â

Is This A Good Question/Topic? 0
  • +

Replies To: Write standard input to file

#2 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: Write standard input to file

Posted 30 January 2012 - 02:13 PM

Have a look at this reference page: stdio.h. I know the website is cplusplus.com but you can still use the reference for the C standard library. I believe you want to look at the function fwrite.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Write standard input to file

Posted 30 January 2012 - 03:37 PM

You're mixing C and C++.

First of all, you named your file with a .cpp extension, which tells your IDE to compile it as C++ code. Then you're using the standard C header stdio.h, and you're using the C FILE input stream and the C function gets (which you should NEVER use, not matter which language -- see below) to read from the file, but then you're trying to use a C++ iostream member function write (which is used in C++ for unformatted output, so you generally shouldn't use that to output ordinary text anyway).

So, if you're supposed to be writing C, rename the program to have a .c name, i.e., myprog1.c. And assuming you're using an IDE, re-set the project properties to indicate that it is a C program (or start over, and specify "C" when you are creating the project).

Then your use of FILE is OK, but for input you should use the fgets function, NOT gets. When you use fgets, you specify the size of your input buffer to avoid a "buffer overrun" error. The gets function is dangerous because it leaves the program vulnerable.

Finally for your output, since you're outputting strings to a text file, use the fputs function.

(If, on the other hand, you intended to write a C++ program, post again, because then you have to take an entirely different approach, from beginning to end.)
Was This Post Helpful? 0
  • +
  • -

#4 praditmodi   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 30-November 11

Re: Write standard input to file

Posted 30 January 2012 - 04:37 PM

I corrected my code as follows :


#include<stdio.h>
void write_stdin();
main()
{
write_stdin();
}

void write_stdin()
{
char input[256];
FILE *p1;
p1=fopen("ofile.txt","w");

while(fgets(input,256,p1))
{

fputs(input,p1);
}
fclose(p1);
}



Thank you for your help ! the program executes but why dont I see my input in the ofile.txt ?
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Write standard input to file

Posted 30 January 2012 - 04:55 PM

Where is your input? If p1 is a pointer to your output file, how can it also be your input file?

edit:
also, main should always return an int, even if you don't use it. Get in the habit of always writing int main.

And also, indent your code so it can be understood more easily. Here's what it should look like. (It's still wrong because of the missing input file, but at least it looks nice. :) )

#include<stdio.h>

void write_stdin();

int main()
{
    write_stdin();
}

void write_stdin()
{
    char input[256];
    FILE *p1;
    p1=fopen("ofile.txt","w");

    while (fgets(input,256,p1))
    {

        fputs(input,p1);
    }
    fclose(p1);
    return 0;
}



This post has been edited by r.stiltskin: 30 January 2012 - 05:00 PM

Was This Post Helpful? 0
  • +
  • -

#6 praditmodi   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 30-November 11

Re: Write standard input to file

Posted 30 January 2012 - 04:59 PM

Posted Image

Isnt this write ?
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Write standard input to file

Posted 30 January 2012 - 05:03 PM

I just realized, you probably meant to be reading input from stdin (the keyboard), and not from a file. In that case the input line should be

while (fgets(input, 256, stdin))

and then you'll use ctrl-z or ctrl-d (depending on your operating system) to signal the end of the input.

Or, if you want to supply input as a command line argument, read this tutorial about command-line arguments.

This post has been edited by r.stiltskin: 30 January 2012 - 05:07 PM

Was This Post Helpful? 1
  • +
  • -

#8 praditmodi   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 30-November 11

Re: Write standard input to file

Posted 30 January 2012 - 05:17 PM

:bananaman: Thanks a million for the help ! ! IT is working now ! :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1