[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

small c programs help using write(), fopen, fdopen, etc...



   Hello...I have been working on the following mini- programs programming in c in unix.  I am a beginner to the c programming field...and am experimenting trying to get a better feel for it.  Right now I am writing the following three small C programs which basically write the string "I love c!" in a file specified by user in the first command-line argument (ex: after compiling...user types 'a.out cool' and upon typing 'cat cool' sees the line "I love c!")  I am trying to do this in several different ways...including the following:

1) using open() and write():

#include <stdio.h>            /*for fopen(), fprintf(), etc. */
#include <fcntl.h>

int main(int argc, char* argv[]) {
  int outputFD;                 /*file pointer to output file */
  outputFD= open(*++argv, O_WRONLY);  /*open file designated by arg[1]*/
  write(outputFD, "I love c!\n", 13); /*write text to file */
  close(outputFD);             /*close file designated by arg[1] */
  }

this is what I have so far and I'm totally stumped and don't know how to fix it to compile correctly.

2)using open(), fdopen() and fputs():
#include <stdio.h>            /*for fopen(), fputs(), etc. */
#include <fcntl.h>

int main(int argc, char* argv[]) {
  FILE* outputFD; /*file pointer to output file */
  outputFD= open(*++argv, O_WRONLY);  /*open file designated by arg[1]*/
  fdopen(outputFD, "w");
   fputs("cs311!\n", outputFD);  /*write text to file */
   fclose(outputFD);             /*close file designated by arg[1] */
}            

Once again totally stumped....not really familiar with using fdopen....

and finally 3) using fopen(), fileno() and write()
however this one I don't have code for since I have absolutely no clue how to get there...since both write() and fileno() are foreign concepts to me...especially when used in conjuction....

Any help solving any of the three or all three would be much appreciated...thank you....


0
ninjacookies
Asked:
ninjacookies
  • 10
  • 5
1 Solution
 
rbrCommented:
int main(int argc, char* argv[]) {

int outputFD;
FILE *outputFP;
                   
outputFD= open(*++argv, O_WRONLY);  /*open file designated by arg[1]*/
                                     
outputFP =fdopen(outputFD, "w");
                                       
fputs("cs311!\n", outputFP);  /*write text to file */
                                       
fclose(outputFP);             /*close file designated by arg[1] */
                                   
}              
0
 
rbrCommented:
fdopen make an filepointer out of a filedescriptor
0
 
ninjacookiesAuthor Commented:

  Thanks rbr for the try....and you cleared up a few things...however, your code for the fdopen prog. only compiled and did not produce the correct output (tried typing 'a.out cool' to create a file named cool with the text written inside but it never created the file.  For full points I'm also looking for solutions on all three...if possible...thanks once again....  
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 
rbrCommented:
outputFD= open(*++argv, O_WRONLY|O_CREAT);  /*open file designated by arg[1]*/
                                     
0
 
rbrCommented:
int main(int argc, char* argv[]) {
int outputFD;
FILE *outputFP;

outputFP=fopen(*argv++,"w");
outputFD=fileno(outputFP);
write(outputFD, "I love c!\n", 13);
fclose (outputFP);
}
0
 
rbrCommented:
outputFP=fopen(*argv++,"w");
should be
outputFP=fopen(*++argv,"w");

 
0
 
ninjacookiesAuthor Commented:

getting very very close there...thanks a lot for the extra info...don't know if you are still working on it...

1)still can't get the fdopen() prog to work despite the O_CREAT addition...compiles fine but doesn't produce a file upon execution.  As for the fileno() program...flawless...thanks.
2) Do you think you could give me that open() and write() prog too if you could? thanks bro...points are just about yours....

p.s. : what does the 13 represent in the following line:
write(outputFD, "I love c!\n", 13);
and how do you know what number to use in other circumstance?
0
 
ninjacookiesAuthor Commented:
oops...accidentally gave you the points for the answer bro...you deserve it though for the hardwork...you think you could do those last things for me anyways though? it would be GREATLY appreciated....thanks
0
 
rbrCommented:
13: Write 13 characters on the file
Try
 outputFD= open(argv[1], O_WRONLY | O_CREAT) ;
in both programms 1 and 2. Can you check the returnvalue of open pls

printf ("%d\n",outputFD);
0
 
ninjacookiesAuthor Commented:
sorry bro...still no luck....outputFD returns a value of -1 when I insert that line....I just can't figure out what's wrong...thanks....
0
 
rbrCommented:
#include <errno.h>


printf ("%d\n",errno)
0
 
jjmcdCommented:
if ( outputFD < 1 )
  {
  perror("On open");
  return 0;
  }
0
 
rbrCommented:
Can you pls post the output of
printf ("%d\n",errno)
0
 
rbrCommented:
Pls give any feedback.
0
 
rbrCommented:
are you online?
0
 
ninjacookiesAuthor Commented:
already got the answer i needed rbr thanks...owe ya the points...
0

Featured Post

The Lifecycle Approach to Managing Security Policy

Managing application connectivity and security policies can be achieved more effectively when following a framework that automates repeatable processes and ensures that the right activities are performed in the right order.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now