Thread: Quick question (if statements)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Quick question (if statements)

    Let's say I have a text file

    (1st line) 234,120,4521,93

    (2nd line) 1, -100

    (3rd line) 4, -40

    (4th line)1, -49

    Code:
     FILE *fp=fopen(argv[1],"r");              
    
     fscanf(fp,"%d%d%d%d",&piels,&coors,&bud,&iron_city); // This is the first line of the text file (234,120,4521,93)         
     
    while(fscanf(fp,"%d%d",&brand_id,&transaction)!=EOF)         
    {                
         if(brand_id==1)               // This is the second line (1, -100) where brand_id=1 and transaction=-100               
      // add -100+-49        
    }
    And i want to add the values of -100 and -40 when conditions are met. How would i do that?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly is the format of the file? Or are you saying that the file will only contain those 4 lines (but with different values)?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tmac619619 View Post
    And i want to add the values of -100 and -40 when conditions are met. How would i do that?
    In the comment of yours you say -49 but know -40.Let's say that -49 is what you want.
    You open the file as you do,then from this line
    Code:
    while(fscanf(fp,"%d%d",&brand_id,&transaction)!=EOF)....
    transaction gets the value -100,so then if you write
    [code]transaction += -49;[code] this will do the operation you want.
    My guess is that your actual question is how to write data back to file.If my guess is correct ,then follow this procedure (after you are done with reading what you want from the file and from modifying the values you want e.g. transaction)
    • close the file with fclose
    • open the same file with fopen and second parameter "w" .This was you are going to overwrite what you already have inside the file
    • use frpintf for example to write back to file
    • close again the file with fclose

    Hope this helps

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About Or Statements
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2009, 01:44 PM
  2. While Statements Question
    By thekautz in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2008, 02:48 PM
  3. If statements question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2008, 04:09 AM
  4. Quick question concerning switch statements
    By Shaun32887 in forum C++ Programming
    Replies: 22
    Last Post: 08-02-2008, 11:33 AM
  5. quick question of curiosity about return statements
    By bobthebullet990 in forum C Programming
    Replies: 1
    Last Post: 08-31-2007, 07:56 AM