Results 1 to 5 of 5

Thread: CSV-File

  1. #1
    Join Date
    Jul 2017
    Posts
    3
    Thanks
    2

    Default CSV-File

    Hello!

    I have a csv file:
    1, 2, 3, 4,
    5, 6, 7, 8,
    with 4 columns and int values on each column.
    I read it line by line.

    I'd wish now to take the first and the second value from each line, and give those values to a struct, and then return a QStringList of those Structures.
    Qt Code:
    1. typedef struct Coordinates{
    2. float latitude; //this will be 1 and 5
    3. float longitude; // this will be 2 and 6
    4. }t_Coordinates;
    To copy to clipboard, switch view to plain text mode 

    I dont know how to acces specific values of a line.
    I'm sorry if I didn't make this thread properly, I'm very new to this, and I am still learning.

  2. #2
    Join Date
    Jul 2017
    Posts
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: CSV-File

    You could use a for loop to navigate through each line and store each value in an array/vector, then access to the 2nd or 4th element


    Qt Code:
    1. vector<float> exmpleArray;
    2. for (int i = 0; i < rows; i++)
    3. {
    4. for (int j = 0; j < columns; j++)
    5. {
    6. exempleArray.pushback(column[j]);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    It would look like this.

  3. The following user says thank you to Alex2822 for this useful post:

    durus90 (26th July 2017)

  4. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    13
    Thanked 153 Times in 150 Posts

    Default Re: CSV-File

    If your input file only contains numbers and commas (i.e. no quoted strings), then you could simply split the string on the comma... See QString::split() and QStringList.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #4
    Join Date
    Jul 2017
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: CSV-File

    Properly reading a general CSV file (which can contain quoted strings) is a bit more complicated. If a comma is preceded by an odd number of quotation marks, it is part of a string, not a separator. And if a quotation mark is immediately after another quotation mark, which is preceded by an odd number of quotation marks, then it too, is part of a string. If you are reading CSV files that may contain quoted strings, you should do this. If not, just split at commas.

    Depending on how precisely you need to locate a point on the earth, float may not be precise enough. I use 32-bit int for most angles, which locates a point to the nearest 18 mm on a meridian and 18 mm on the equator (the precision on other parallels varies with latitude). For more precise positioning, I use double, expressing both angles in radians.

  6. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    13
    Thanked 153 Times in 150 Posts

    Default Re: CSV-File

    I certainly understand that. The OP's original post showed simple numbers in the input, which is why I prefaced my reply with "If your input file contains number and commas (i.e. no quoted strings)...".
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 2
    Last Post: 13th January 2014, 07:27
  2. Replies: 4
    Last Post: 2nd October 2013, 13:57
  3. Replies: 3
    Last Post: 1st November 2010, 16:33
  4. Replies: 3
    Last Post: 28th March 2009, 15:37
  5. Replies: 3
    Last Post: 25th May 2007, 07:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.