Thread: Manipulating data in a FILE

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    34

    Manipulating data in a FILE

    How can I manipulate a data in a file for example the saved file is like this:

    code name Price Quantity
    001 hammer 100 4
    002 screwdriver 100 4

    what i like to do is:
    1. the user will be ask to enter its code
    2. will output the searched code and then
    3. the user will be ask to enter how many pcs
    he/she will buy.
    the formula should be

    pcs=pcs-users_input

    if the user selected the code 001 and enters only 1pc it should be updated to only 3 pcs left in the file.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    So where's your code attempt?

    You should know by now that you can't just dump your assignment and expect code.
    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
    Sep 2014
    Posts
    34
    i don't know how to do it... that's why i'm asking...

    just an example or link will do Mr. Thanks.
    i do have a code here but i don't understand how it works....

    Code:
     fp=fopen("prod.txt","r+");
                                while(!feof(fp))
                                {
                                                fgets(record,80,fp);
                                                drugs=strtok(record,";");
                                                strcpy(num[x],drugs);
                                                drugs=strtok(NULL,";");
                                                strcpy(name[x],drugs);
                                                drugs=strtok(NULL,";");
                                                strcpy(gen[x],drugs);
                                                drugs=strtok(NULL,";");
                                                strcpy(clas[x],drugs);
                                                drugs=strtok(NULL,";");
                                                strcpy(manuf[x],drugs);
                                                drugs=strtok(NULL,";");
                                                strcpy(price[x],drugs);
                                                drugs=strtok(NULL,"");
                                                x++;
                                }
    something like this...

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Some pointers to get you going

    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com (There is also good sample code here)

    There is a good sample of strtok at the end of this msdn page:
    strtok, wcstok, _mbstok
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    34
    Quote Originally Posted by Click_here View Post
    Some pointers to get you going

    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com (There is also good sample code here)

    There is a good sample of strtok at the end of this msdn page:
    strtok, wcstok, _mbstok

    What is the use of the putchar() and fgetc()

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > i do have a code here but i don't understand how it works....
    Well it would be better if you tried to write your own code - you know, one which parses 4 fields separated by spaces, as opposed to 7 fields separated by semicolons.

    Seriously, read this
    A development process

    And if you get stuck, don't be afraid to create a separate project to test specific ideas.
    Not sure how strtok() works, then write a 5-line program to test it!
    Code:
    int main ( ) {
      char line[] = "this is a line\n";  // this is what fgets() would store
      char *token = strtok(line," ");
      printf("Line=%s, token=%s\n", line, token);
      token = strtok(line,NULL); // NULL means carry on from last time
      printf("Line=%s, token=%s\n", line, token);
    }
    Read the manual page, play around with the function - make sure you know how it works!

    Don't just go round googling for code fragments, pasting bits of what you find on another forum, then asking people to basically rewrite it to match your assignment.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-17-2013, 02:19 PM
  2. I/O files: reading, manipulating and outputting data
    By Mwepak in forum C++ Programming
    Replies: 17
    Last Post: 12-12-2012, 07:50 AM
  3. Manipulating data in an array...
    By MikeyIckey in forum C Programming
    Replies: 11
    Last Post: 01-03-2009, 03:23 PM
  4. NOOB: Need a little help opening file and manipulating data
    By liquidcourage1 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2006, 10:27 PM
  5. Manipulating Text File?
    By Silverdream in forum C Programming
    Replies: 1
    Last Post: 03-22-2002, 06:44 PM