Thread: I'm too lazy to think of a decent thread title, or to bother formatting the code

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    Exclamation I'm too lazy to think of a decent thread title, or to bother formatting the code

    Well im trying to create a program to do a program for a store, but im stuck when trying to input and read prices from a text file. As you can see from the menu i have alot to do. Any help and advice would be appreciated. I put the main() infront of the change price function to see if it was working.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    /*char menu
    {
         int choice
         
         while(choice != 'e') 
         
         printf("Welcome to the REGISTER 2000\n");
         printf("Choose the corresponding letter to choose the option you desire");
         printf("a. Payment\n");
         printf("b. Change price\n");
         printf("c. Veiw stock\n");
         printf("d. Add stock\n");
         printf("e. Exit");
         
         fflush(stdout);
         
         scanf("%c", &choice);
         
         }
         
         
    int password
    {
    char userid[]="zion",password[]="zionatscodz",p[15],u[15];
    int n=1,a,b;
    printf("Enter USER ID and PASSWORD below (You have only three chances to enter)\n");
    getchar();
    while(n<=3)
    {
     
    printf("\nUSER ID: ");
    scanf("%s",u);
    printf("\nPASSWORD: ");
    scanf("%s",p);
    a=strcmp(u,userid);
    b=strcmp(p,password);
    if(a==0&&b==0)
    {
    printf("You have logged in successfully.\n");
    break;
    }
    else
    {
    printf("Wrong PASSWORD and/or USER ID. Now you have %d more chances ", 3-n);
      
    }
    getchar();
    n++;
    }
    if(n==4)
    printf("You can't log in.\n");
    getchar();
    }
    
    */
     
    main()
    {
    int i;      
    int price;
    char item[10];
    
    FILE * fp =fopen("price list.txt","a");
    if(!fp)
          {
              printf("the file could not be found, create a file\n");
              getchar();
              return 1;
          }
    printf("Input prices\n");    
    for (i=1;i<=10;i++)
    {
    fscanf(stdin,"%s%d",item,&price);
    fprintf(fp,"%s%d",item,&price);
    }
    fclose (fp);
    fprintf(stdout,"\n\n");
    fp=fopen("price list.txt","a");
    printf("Item  price");
    for(i=1;i<=10;i++)
    {
    fscanf(fp,"%s%d%",item,&price );
    
    fprintf(stdout,"%s%d\n",item, price);
    }
    fclose(fp);
    system("PAUSE");
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you open the file in append mode, you can't then fscanf() something from it - you're already at the end of the file.

    Code:
    fp=fopen("price list.txt","a");
    printf("Item  price");
    for(i=1;i<=10;i++)
    {
    fscanf(fp,"%s%d%",item,&price );
    
    fprintf(stdout,"%s%d\n",item, price);
    }

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    so i would have to put "r" instead of "a"??

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you want to read data from a file, then yes - open it in read ("r") mode. I prefer the explicit "rt" however, instead of just "r".

    "r" will use the text or binary mode that is set by an environmental variable or other config file for your compiler.

    somebody else may not have that configuration - I don't, for one.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    actully sorry i want to write to it, but when i try to it doesnt work

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Ah, the "it doesn't work bug". We fixed that last week. Please search the forum.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    fscanf(fp,"%s%d%",item,&price );
    I think that extra % might be giving you grief.

    By the way: fprintf(stdout, ...) is exactly the same as printf(...), and fscanf(stdin, ...) is exactly the same as scanf(...).

    FWIW, you're also missing some semicolons and curly braces in some of that commented-out code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Adak View Post
    When you open the file in append mode, you can't then fscanf() something from it - you're already at the end of the file.

    Code:
    fp=fopen("price list.txt","a"); 
    /* you've opened the price list.txt file, and put the file pointer at the very end of it*/
    
    
    
    printf("Item  price");
    for(i=1;i<=10;i++)
    {
    
    /* now you're trying to read from the price list.txt folder - and you'll find 
    nothing, because you're already at the end of the file */
    fscanf(fp,"%s%d%",item,&price );
    
    
    //for the love of Mike - use printf() instead of fprintf(stdout)
    fprintf(stdout,"%s%d\n",item, price); 
    }
    When you get your file mode coordinated with your file operation, your data will be read and written OK.

    You're close, David!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 10-03-2002, 03:04 PM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM