Thread: Theoretical question regarding text files

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    21

    Theoretical question regarding text files

    Hello,
    I was just wondering whether or not it is possible to "mix" characters and integer values in a text document that is to be read by C.

    I am to read and use values for length around the hip, neck and such, however, most of the tutoring examples I am finding online seem to strictly deal with either integers or characters alone.

    Basicly my text file looks a bit like:
    Abdominal length: 90.000
    Neck length: 26.500
    and so on

    So, before I move further into this subject, is it possible at all to isolate and use only the double values or will I have to format my text file differently?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You can have whatever type of data you want in a file, and mix text, numbers, binary data, etc. As long as you know how it is stored, you can extract the parts you want. In your case, it wouldn't be too difficult. I would read in a line with fgets. Then, I would either use sscanf or a combination of, say strtok and strtod, to extract the double values.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    Right, I get the logic behind that. I can make it read and scan the first line of my text document succesfully.
    How would you make it switch to the next line and repeat the process? A loop with a condition like textfile != EOF or something like that?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    > How would you make it switch to the next line and repeat the process? A loop with a condition like textfile != EOF or something like that?
    Something like that...vaguely. Exactly how depends on the file reading functions you use, and I can't list them all. Any good tutorial or textbook should show you several ways, so start by reading some textbooks and tutorials on file handling and loops. Then, read the documentation for the function you want to use to read from the file (e.g. fgets). Also read this link: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com.

    Then, give it your best attempt. If you get stuck with something specific, post back with your code and we can help you more.

    EDIT: You should get in the habit of trying little experiments for stuff like this. The worst that happens is your program crashes, best case it works. You'll learn much more by trying/experimenting yourself than if you come here straight away for all your questions.
    Last edited by anduril462; 07-23-2013 at 02:42 PM.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    Quote Originally Posted by anduril462 View Post
    EDIT: You should get in the habit of trying little experiments for stuff like this. The worst that happens is your program crashes, best case it works. You'll learn much more by trying/experimenting yourself than if you come here straight away for all your questions.
    Yeah, thats what I've heard is the best way to learn, which I've also tried to practice.. I made other adjustments for my program this evening and just needed to lay it off for a bit, so I thought a healthy brainstorm and discussing here might do the trick before I go to bed. I'll pick it up in the morning.

    Thank you!

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by TobiasK View Post
    Hello,
    I was just wondering whether or not it is possible to "mix" characters and integer values in a text document that is to be read by C.
    Yes and no. A file is either binary or text. If it's binary, you must pass "rb" to open it, or any cr/lf sequences will be silently changed to just an lf.
    You can't embed binary objects like doubles in text. You have to convert them to human-readable ascii using fprintf() / fscanf() or similar. But you can embed text in binary files. You need to add the nul or some other sentinel at the end of a string, then write a special routine to read it.
    Binary files tend to be easier to parse than non-binary, and they are also more compact. But they are not portable. A double on your machine might not have the same bits as a double on another.

    (If you're interested in reading and writing binary doubles completely portably, go onto my website and look at the matlab file exporter. Matlab uses raw binary, so I read and write them transparently).
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    What am I doing wrong here? It seems so simple, yet, it doesn't work.

    Code:
        printf("%s", tekst); /* tekst is a string "Body fat percent is: 24.325" */ 
        sscanf(tekst, "%lf", BFpercent); /* Want to scan the string for an accurance of a double and store it in BFpercent */
        printf("%lf", BFpercent); /* BFpercent comes out as 0.000 */

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You should skip the beginning of the string

    "%*[^:]: %lf"

    Will read till the : ignoring it and not storing anywhere, then skip : and space and then start reading the double

    PS. Also sscanf should get pointer to BFpercent to be able store the data into var
    Last edited by vart; 07-26-2013 at 11:11 AM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Compile with warnings turned all the way up, you have an error somewhere. I can't tell without seeing the definition, but either your sscanf or the printf of BFpercent is wrong. printf takes a double to use with "%lf". I think it's the sscanf that's wrong, you need a & in front of the BFpercent so scanf gets the address of BFpercent, and knows where to store the value it scans.

    Also, check the documentation for scanf (emphasis mine): sscanf(3): input format conversion - Linux man page
    Quote Originally Posted by man sscanf
    RETURN VALUE These functions return the number of input items successfully matched and assigned, which
    can be fewer than provided for, or even zero in the event of an early matching failure.
    You should check the return value of sscanf and ensure it works before proceeding, and assuming BFpercent has a value:
    Code:
    if (sscanf(tekst, "%lf", &BFpercent) != 1) {
        // could not scat BFpercent, handle error -- perhaps print something and exit
    }
    Lastly, sscanf doesn't just skip ahead to any double. The first thing in the format string is a %lf, so sscanf expects the first thing in tekst to be a double. It's not, so the match fails. If you know the start to tekst will be fixed, put it in the sscanf literally:
    Code:
    if (sscanf(tekst, "Body fat percent is: %lf", &BFpercent) != 1)

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    Oooh yeah, bugger!

    Forgot that tiny & in the scan. All the small things! It seems to work now, after implementing vart's code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about text files and strings
    By Gabriel Sotero in forum C Programming
    Replies: 1
    Last Post: 03-02-2012, 07:36 PM
  2. Please help me in this theoretical question of C++ Templates
    By umair_attock in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2009, 09:28 PM
  3. Question About Blank Lines in Text Files
    By Zildjian in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 04:31 PM
  4. theoretical Q about arrays
    By Nutka in forum C Programming
    Replies: 3
    Last Post: 10-03-2002, 05:47 AM
  5. theoretical problem
    By cozman in forum C++ Programming
    Replies: 8
    Last Post: 09-06-2001, 04:02 PM