Search and replace question

This is a discussion on Search and replace question within the C Programming forums, part of the General Programming Boards category; Let's say I have a file column that looks like this: Line# Value 1 6 2 6 3 6 4 ...

  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
    Katy, Texas
    Posts
    2,309
    I don't see a question.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    457
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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
    457
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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, 11:04 AM
  2. vector search and replace
    By rahulsk1947 in forum C++ Programming
    Replies: 6
    Last Post: 06-04-2009, 10: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-13-2002, 12:28 AM
  5. Search and replace coding....
    By bishop_74 in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2001, 03:22 PM

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