Help: how to read data file seperated by comma

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xuxf055
    New Member
    • Jan 2007
    • 6

    Help: how to read data file seperated by comma

    Hi
    Everybody,
    I am confused for a few days, how to read the data file seperated by comma.
    for exampe following file.
    data.txt
    1,2,3,4,5,6,7,8 ,9,10
    11,12,13,14,15, 16,17,18,19,20
    how to read it inot an array or display them on screen?
    thanks
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Are you using C++? If so, and you are using the ifstream class to read from the file, you can use the .ignore() function.

    Since you know all your values are separated by commas, you can 1st read in the value (in your example integers) and then use .ignore(1) to skip the next character (the comma). If there are spaces in between the letters and the commas, you may have to use .ignore(100, ',') which will skip all characters until the comma.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      Originally posted by xuxf055
      Hi
      Everybody,
      I am confused for a few days, how to read the data file seperated by comma.
      for exampe following file.
      data.txt
      1,2,3,4,5,6,7,8 ,9,10
      11,12,13,14,15, 16,17,18,19,20
      how to read it inot an array or display them on screen?
      thanks
      if you are using C you can use scanf() so
      Code:
      scanf("%d,",&x);
      where %d converts a decimal integer and the , checks that the next character in the input stream is a , and skips it
      http://www.cplusplus.c om/reference/clibrary/cstdio/scanf.html

      Comment

      • xuxf055
        New Member
        • Jan 2007
        • 6

        #4
        Thanks for your help!!


        Originally posted by horace1
        if you are using C you can use scanf() so
        Code:
        scanf("%d,",&x);
        where %d converts a decimal integer and the , checks that the next character in the input stream is a , and skips it
        http://www.cplusplus.c om/reference/clibrary/cstdio/scanf.html

        Comment

        Working...