Thread: r/w query

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    36

    r/w query

    Hi guys

    As you may be aware I'm reading in a text file pre writting this out again but with a header added to it.

    The input text files can be quite large in size (read this as number of lines) so I need to get this read / write to be as quick as possible. Experimentation has found that using the fstream approach is very slow compared to using the c fscanf approach, so I'm using the fscanf ,ethod

    However, as you can see from the following code I'm reading in each part of the input file into 3 variables. How can I read the whole line at once into a single string, thereby saving tiime?

    Code:
    float x,y,z
    fscanf(finput,"%f %f %f",&x,&y,&z);
    Is there also a way I can just read in the fist character of a line (I assume I could use fgetc), but how do I then jump to the next line?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use fgets() to read the line.
    If you want to process it, then use sscanf(buff,"%f %f %f",&x,&y,&z);
    If you just want to look at the first char, then that's in buff[0]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    36
    Use fgets() to read the line.
    If you want to process it, then use sscanf(buff,"%f %f %f",&x,&y,&z);
    If you just want to look at the first char, then that's in buff[0]
    So assuming I just wanted to increment a counter, thereby giving a total num of lines when I reach the eof whats the fastest approach? use fgets or fscanf.

    In this situation I'm not worried about using the data, just need to do a read of a line?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple query language
    By afflictedd2 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2008, 05:29 PM
  2. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  3. Url query encoding/decoding
    By Niara in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-25-2007, 03:30 PM
  4. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  5. dns query
    By bulldog in forum C Programming
    Replies: 6
    Last Post: 02-24-2004, 10:44 AM