Thread: I need help with this code

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    I need help with this code

    The code is supposed to define a file pointer called "fpwrite"
    open a text file called "sppdata.txt"
    and check to see if the file is opened and use it to write "Hello Sara" a hundred times. I am missing a couple of things I think.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're missing a for loop which loops 100 times, and a fprintf() call inside that loop
    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.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(void)
    {
    
        char name[31] = "Hello Teri";
        FILE *fpwrite;
        int i;
        
        fpwrite=fopen("cppdata.txt" , "w");
        
        if (fpwrite==NULL)
        {
            printf("cannot open or create file.\n");
        }
        else
        {
    
            for (i=0;i<100;i++)
            {
                fprintf(fpwrite,"%s\n",name);
            }
            fclose(fpwrite);
    
        }    
    
        return 0;
    
    } /*main*/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM