File Output problem

This is a discussion on File Output problem within the C Programming forums, part of the General Programming Boards category; Hi,i need help with my book database.I use scanf to store book name,author name and year when is book writen. ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    File Output problem

    Hi,i need help with my book database.I use scanf to store book name,author name and year when is book writen.

    example:
    Code:
    scanf(" %[^\n]",k[i].bookname);
    I have problem when i want to printf all books from database with this:

    Code:
    for(i=0;i<counter;i++){                  
    fscanf(f,"%s %s %d",k[i].bookname,k[i].author,&k[i].year);
    printf("\n%d\t%s\t%s\t%d",(i+1),k[i].bookname,k[i].author,k[i].year);}
    Output:

    Number Book name Author name Year
    1 King John 0
    2 William Shakespeare 1590

    How can i fix this?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Show more code. Particularly, all the code that loops through and stores the data into the structure and all variables/declarations related to said structure.

    Next, describe the input you've given to produce the above output and how it differs from what you expected when output.

    Right now you've given us some code and output and simply said "its wrong how do I fix it?" We can guess that the book name not showing up is a problem but without more of the scanning code and what specific input you entered we are hindered.
    "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

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    My code is in Croatian...
    I type full book name with spaces in bookname,name and surname of author in author,and year in year with this:

    Code:
    printf("\nHow many books do you want to type in?\n");
    		scanf("%d",&n);
    		for(i=0;i<n;i++){
                fseek(f,0,SEEK_END);
    			printf("\nWrite in the name of %d. book:\n",i+1);
    			scanf(" %[^\n]",k[i].bookname);
    			printf("\nWrite in the name of %d. author:\n",i+1);
    			scanf(" %[^\n]",k[i].author);
    			printf("\nWrite the year of %d. book:\n",i+1);
    			scanf("%d",&k[i].year);
    			fprintf(f,"%s %s %d\n",k[i].bookname,k[i].author,k[i].year);}
    and when i open file it look like this:
    King John William Shakespeare 1590
    Rome and Juliett William Shakespeare 1600

    but when i want to printf all books from file with this code:
    Code:
    for(i=0;i<counter;i++){                 
    fscanf(f,"%s %s %d",k[i].bookname,k[i].author,&k[i].year);
    printf("\n%d\t%s\t%s\t%d",(i+1),k[i].bookname,k[i].author,k[i].year);}
    I get output like this
    Number Book name Author name Year
    1 King John 0
    2 William Shakespeare 1590


  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > King John William Shakespeare 1590
    > Rome and Juliett William Shakespeare 1600
    How is the vanilla %s supposed to tell the difference between the space between "King" and "John" as being part of the title, and the space between "John" and "William" as separating the title from the author.

    > scanf(" %[^\n]",k[i].bookname);
    You already recognise that %s breaks at spaces, so you do this.

    > fprintf(f,"%s %s %d\n",k[i].bookname,k[i].author,k[i].year);
    Then you introduce more spaces making the whole file confusing.

    You need to make your file unambiguous, say perhaps
    fprintf(f,"%s\n%s\n%d\n",k[i].bookname,k[i].author,k[i].year);
    where you can use the same style scanf calls you used to read from stdin to begin with.

    Or say
    fprintf(f,"%s,%s,%d\n",k[i].bookname,k[i].author,k[i].year);
    fprintf(f,"%s\t%s\t%d\n",k[i].bookname,k[i].author,k[i].year);

    where you introduce some kind of separator which doesn't appear in your input data.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Oh so stupid mistake,didnt think about that .. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in Viewing Output File in VMD
    By benner in forum Tech Board
    Replies: 2
    Last Post: 03-09-2012, 09:14 PM
  2. file output problem
    By S0n1C in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2007, 12:51 AM
  3. Replies: 3
    Last Post: 10-20-2006, 07:59 PM
  4. File output problem using ofstream
    By yatta! in forum C++ Programming
    Replies: 1
    Last Post: 12-23-2002, 04:05 AM
  5. File input output problem
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2002, 02:09 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21