Quick question (if statements)

This is a discussion on Quick question (if statements) within the C Programming forums, part of the General Programming Boards category; 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, ...

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

    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
    21,737
    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)?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,681
    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, 01:48 PM
  3. If statements question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2008, 03: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

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