Thread: Question about fputs

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Question about fputs

    With fputs, is there a way to adjust where its puts the data? I need to put data in a column in another file.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Sure. You just change the second argument. For example, fputs("slappy", stdout); will write to the standard output device, whereas fputs("slappy", fp); will write to whatever fp points to.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    I understand that but I need to put "slappy" in about 30 spaces to the left of the margin of the file that fp points to.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    {
      int i;
    
      for(i = 0;i < 30;++i)
        fputc(' ', fp);
      fputs("slappy", fp);
    }
    Or you can just prepend 30 spaces to the string. Or you can put a 30-character string filled with spaces in the file before writing "slappy". There's a dozen different ways to do it.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Cool, thanks!!!

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can't just use fprint with a width specifier instead of using fputs?

    Code:
    // Write "slappy" with 30 spaces in front of it.
    fprintf(fp,"%*s",30+strlen("slappy"),"slappy");
    I'm just saying, is it a requirement that you use fputs and can't use fprintf?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    sprintf is your friend. Followed by a simple:
    Code:
    fputs( buf, fp );
    Assuming of course that you don't simply just use fprintf in the first place.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM