Thread: Problem with writing in a file using fprintf

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    Problem with writing in a file using fprintf

    Good morning!

    I have a problem using fprintf.
    I try to print a linked list which consists of nodes(structs).
    When i try to print the list to the standard output using printf evrything is ok
    e.g. i do :

    printf("%d, %d, %s, %s\n",p->l, p->t, p->a, p->b);

    //p is pointer to the struct

    Now i try to print the linked list in a file i do:
    Code:
    if( ( fp = fopen(argv[1],"w") ) == NULL )
                            printf("File couldn't be opened\n");
                    else {
                            p = head;
                            while( p != NULL) {
                                    fprintf(fp, "%d, %d,%s,%s\n",p->l, p->t, p->a, p->b);
                                    p = p->next;
                                            }
                            }
                     fclose(fp);


    But nothing is being typed to the file!
    What do i do wrong?

    Thanks in advance!

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well nothing looks to me wrong over in the above code. Post use some more code.

    Need proper indendation
    Code:
    if( ( fp = fopen(argv[1],"w") ) == NULL )
        printf("File couldn't be opened\n");
    else 
    {
         p = head;
         while( p != NULL) 
         {
                fprintf(fp, "%d, %d, %s, %s\n",p->l, p->t, p->a, p->b);
                p = p->next;
         }
    }
    fclose(fp);
    ssharish2005

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Well the problem is with that fprintf,
    The other program is fine as i can see
    cause when i try to print the list using the printf
    the list is printed!

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well your fprintf statment is right, it follows the synatx and looks to me right

    ssharish2005

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Well, now it works!
    I don't know why it works now,I just changed the input file i was giving previously!
    Now everything seems fine for the time being!

    Thank you!

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by g_p View Post
    Well, now it works!
    I don't know why it works now,I just changed the input file i was giving previously!
    Now everything seems fine for the time being!

    Thank you!
    You shouldn't feel satisfied just because the problem inexplicably disappears. Go back to file that wasn't working and figure out why.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Problem in writing string to File
    By Bargi in forum C Programming
    Replies: 4
    Last Post: 02-18-2009, 12:05 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM