Thread: file select row

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Luxembourg
    Posts
    5

    file select row

    Hey guys,
    how do I put the cursor at the beginning of a certain row,
    for example to read the fifth entry of a csv file?

    Thanks,
    Jeff

    (deleted in edit)
    Last edited by De_Lėtzeboier; 02-03-2008 at 11:55 AM. Reason: unappropriate

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, can't reply in bad english, or in French or German, but I'll try in "simple english". Also, this is an english speaking forum, so discussing in other languages is probably not the most appropriate.

    CSV files (as standard) don't have a fixed size per "row", so you need to basically count the number of rows as you read through them. It is possible to store the starting position of each row, should you wish that, and "remember where a row is".

    Using fgets() to read each line is the simplest form.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Location
    Luxembourg
    Posts
    5
    Actually, "simple english" is what I prefer, it's most accurate to talk about programming etc.
    However, since this is not a linguistic forum, let's get back to the topic:

    Doesn't fgets() only read 'till a space? So, if I understand well, I shall use fgets() (n-1) x m times, where n ist the cardinal of the corresponding row and m is the number of space-separated strings in each row, right?

    Thanks,
    Jeff
    (and sorry for the languages P.S.)

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    You could loop through four stream.getline()s which should leave you at the start of the fifth line.
    Alternatively, there is a GNU extension, getline(), to cstdio, which you could also use, but of course, it's an extension, therefore non-standard.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, sorry, missed that it was C++ rather than C, so getline() is the right function to use.

    Both fgets() and getline() reads until either the limt of the buffer is reached, or a newline is found, so assuming your data buffer is large enough to read an entire "row", it should be fine to read using either function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Location
    Luxembourg
    Posts
    5
    Ok, thanks to all!
    I'll try it out and if it works, I'll close the thread...if I find out how...

    Jeff

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Threads here are only closed when the moderators think they are "abused". Just responding with "my problem solved" usually is the "end" of the thread.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Location
    Luxembourg
    Posts
    5
    k
    thx

  9. #9
    Registered User
    Join Date
    Feb 2008
    Location
    Luxembourg
    Posts
    5
    my problem solved
    thx, good forum(Y)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM