Reading a record from a File

This is a discussion on Reading a record from a File within the C Programming forums, part of the General Programming Boards category; Ok, quick question about reading a record from a file. The format of the record is: qty;cost;stockNumber;description: The first two ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    19

    Reading a record from a File

    Ok, quick question about reading a record from a file.

    The format of the record is:

    qty;cost;stockNumber;description:

    The first two are ints and the last two are chars.

    This how I'm reading the record:

    Code:
    while(fscanf(fp,"%d;%d;%[^;];%[^:]:", &q, &c, stNum, desc) == 4)

    Right now it is only reading up to the : (colon). The problem is that I want the colon to be included at end of the description. I tried a few things and I only get the first record go into other variables I have.

    Thanks.

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    The reason why your : is not staying in your fscanf() sequence is this.

    A set format flag is as all flags started with a % followed by a sequence of characters between an open and a closing bracket. For example, %[^:].

    The ^ denotes not or excluding. This specific format flag defines the rule: read the string of all characters until reaching the character defined after ^, i.e. all characters, until the character after ^ matches, will be read into the variable name.

    Side note: Whitespaces will be read in this case. The set format flag is also special in the way that initial whitespaces will not be skipped as it was in the case for %s and for most other format flags. To skip initial whitespaces you can preceed the format flag with a space. When scanf sees one or more spaces in the format string, it will match these with any number of whitespaces.

    With this in mind, you cannot simply save the : after the description, or from what I know. You can concatentate or in some other means add it back if you see fit.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What comes after a record? Is there a single record for each line, or does the colon act as a separator? If it's the former then %[^\n] would work better than %[^:]:. If it's the latter then you won't find an elegant way to use the colon as both a terminator and include it in the string with a single call to scanf and no helpers.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. reading a customer record file
    By abbycat131 in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2003, 09:15 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21