Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6756577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:32:28+00:00 2026-05-26T13:32:28+00:00

I am trying to read a file in a specific file format in c.

  • 0

I am trying to read a file in a specific file format in c.
the file contains some data items. every data item is seprated by a flag.
the file should look look like this:

file-header: "FIL0"
file-id: 0x1020304

flag : 0|1 : uint8_t
length : uint32_t
char[length] : int utf-8

so its: [File-Header] [FileID] [Flag | Length | Data ] [Flag | Length | Data] ...
--> "FIL0" | 0xFFFFFF | 0 or 1 | Data as char[] | 0 or 1 | ... (next data item) ....

My Problem occurs when reading in the file. My idea is to open the file and scan through it using some sscanf-magic.

FILE *fp;
fp = fopen("data.dat". "r");
/* scan file for data components */

  while (fgets(buffer, sizeof buffer, fp) != NULL) /* read in file */
    {
      /* scan for sequence */
      if (sscanf(buffer, "%5s", fil0_header) == 1) /* if the "FIL0" header is found */
        {
          printf("FIL0-header found: %s\n", buffer);

          // proceed and scan for [FLAG] [LENGTH] [DATA]
          // sscanf() 
          if (sscanf(buffer, "%u", node) == 1) 
            {
               // doesnt seem to work 
            }

          // read in length of string and extract stringdata

      else
        {
          printf("FIL0-Header not found, found instead: %s\n", buffer);
          // do something
        }
    }

My problem that I have a hard time with my buffer and the varying data types in the file.

The comparision of fil0-header works alright, but:

  • how to read in the next hexadeciaml number (sscanf using %D)
  • how scan for the flag which is 1 byte
  • how to extract the length which is 4 bytes

A problem is, that the check for the flag starts at the beginning of the buffer.
but the pointer should be moved on, after the FIL0-header is found.
I’d be gratefull for any help!

Please help me to find the proper sscanf() -calls:

and want to read it in and retrieve the single parts of my file:
On single [File-Header]
and many {[FileID] [Flag | Length | Data ]} {…} items

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:32:29+00:00Added an answer on May 26, 2026 at 1:32 pm

    well you could just read the file per byte using

     line[0] = (char) fgetc(fp);
     line[1] = (char) fgetc(fp);
    

    and so on or leave out the cast to retrieve an int-value… should do the trick to do an easy right to left scan of the file (or line – as you say there arent any line breaks)…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read specific data from a file into two 2D arrays.
I'm trying to read a text file in a specific package but it return
i have a big file in text format who i trying to read through
I'm trying to read a specific line from a file and I can get
I'm trying to read a specific section of a line out of a file
In VB.net I'm trying to read in a specific line from a file. An
I'm trying to read specific values from the text file (below): Current Online Users:
I'm trying to read some text from an html file, modify it in a
I'm trying to read a file to produce a DOM Document, but the file
I am trying to read from file: The file is multiline and basically i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.