• C

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....


ninjacookiesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ninjacookiesAuthor Commented:
already got the answer i needed rbr thanks...owe ya the points...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.