Thread: Search and replace question

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    Search and replace question

    Let's say I have a file column that looks like this:

    Line# Value
    1 6
    2 6
    3 6
    4 6
    5 6
    6 8


    If there are three or more of the same values in a row, start by replacing the second to first value with a number (say '0') and continue to do this to the numbers until a different value is found. The desired result would look like this:
    Line# Value
    1 6
    2 0
    3 0
    4 0
    5 0
    6 8


    I'm using fscanf to read my file.
    Thanks!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I don't see a question.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    Re

    -sorry. The question is, how would I do this.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Nobody here will help you until you show that you at least tried. Start by working through the logic on paper, and see what you come up with.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    If there are three or more of the same values in a row
    ... do you mean column?

    Judging by your other posts, you know more than enough to at least get started on this. Read in the file, parse out the numbers you care about. You'll easily find algorithms online for finding duplicates if you can't figure it out yourself with a few loops.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    Re

    Perhaps I am a bit confused about how fscanf works. I've read that it reads in the file line by line, correct? Does it hold in memory the value(s) of the previous line? I think that once I understand the logic behind fscanf, it will be easier for me to decide how to handle this problem.

    Thanks.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    fscanf does not read line by line, it read in a specific format that you specify.
    fscanf - C++ Reference

    For line by line reads you want fgets

    fgets - C++ Reference

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You give it a format string (just like with printf) and it attempts to read that string. If the string has a space, it will try and read a space, etc... If you include special characters for, say, decimal numbers, it will try and read a decimal number. In addition to the format string, you pass it address to variables you have prepared to store the values that it reads. For every special symbol or variable in your format string, you must pass a corresponding address for fscanf to write to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. vector search and replace
    By rahulsk1947 in forum C++ Programming
    Replies: 6
    Last Post: 06-04-2009, 09:13 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. IDEA: Search And Replace
    By ygfperson in forum Contests Board
    Replies: 1
    Last Post: 08-12-2002, 11:28 PM
  5. Search and replace coding....
    By bishop_74 in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2001, 03:22 PM