Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Skipping data with fscanf?

Skipping data with fscanf?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jose Luis Sogorb
    wrote on last edited by
    #1

    Hello, I have 80 lines in a text file with 30 values each line separated by a white space (some are ints, doubles and another strings). I have made a routine with fscanf() to read each line and store the values I need. But I only need 7 of the 30 values to store them in an array, so , for instance, I want to store the first data, and the second and third, then skip the next 4 values, store the next and so on. Is there a way to skip entries with fscanf function? Thank you.

    CPalliniC D 2 Replies Last reply
    0
    • J Jose Luis Sogorb

      Hello, I have 80 lines in a text file with 30 values each line separated by a white space (some are ints, doubles and another strings). I have made a routine with fscanf() to read each line and store the values I need. But I only need 7 of the 30 values to store them in an array, so , for instance, I want to store the first data, and the second and third, then skip the next 4 values, store the next and so on. Is there a way to skip entries with fscanf function? Thank you.

      CPalliniC Online
      CPalliniC Online
      CPallini
      wrote on last edited by
      #2

      José Luis Sogorb wrote:

      Is there a way to skip entries with fscanf function?

      Oh yes. Simply read unwanted entries and then discard them. :-D

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      [my articles]

      In testa che avete, signor di Ceprano?

      J 1 Reply Last reply
      0
      • CPalliniC CPallini

        José Luis Sogorb wrote:

        Is there a way to skip entries with fscanf function?

        Oh yes. Simply read unwanted entries and then discard them. :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        [my articles]

        J Offline
        J Offline
        Jose Luis Sogorb
        wrote on last edited by
        #3

        Yes, but I have to read the unwanted data and assign them a place to store them and then discard? There are 23 unwanted entries in each line, and there are 80 lines. This is what I dont want to do.

        CPalliniC 1 Reply Last reply
        0
        • J Jose Luis Sogorb

          Yes, but I have to read the unwanted data and assign them a place to store them and then discard? There are 23 unwanted entries in each line, and there are 80 lines. This is what I dont want to do.

          CPalliniC Online
          CPalliniC Online
          CPallini
          wrote on last edited by
          #4

          Well you haven't accumulate the garbage data. You may use a restricted set of variables (one for each different data type). :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          [my articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • J Jose Luis Sogorb

            Hello, I have 80 lines in a text file with 30 values each line separated by a white space (some are ints, doubles and another strings). I have made a routine with fscanf() to read each line and store the values I need. But I only need 7 of the 30 values to store them in an array, so , for instance, I want to store the first data, and the second and third, then skip the next 4 values, store the next and so on. Is there a way to skip entries with fscanf function? Thank you.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            What's wrong with:

            for (int x = 0; x < 80; x++)
            {
            fscanf(...);
            if (some_condition)
            {
            // store in array
            }
            }

            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups