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

  • Home
  • SEARCH
  • 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 725567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:21:06+00:00 2026-05-14T06:21:06+00:00

Say I have a file with the format: key1/value1 key2/value2 key3/value3 …. Say I

  • 0

Say I have a file with the format:

key1/value1
key2/value2
key3/value3
....

Say I have an array to hold these values:

char *data[10][10]

How would I read this file and get key1, key2, and key3 into data[0][0], data[1][0], and data[2][0]. Then put value1, value2, and value3 into data[0][1], data[2][1], and data[3][1]. So actually I want to get the strings of key1-key3 individually, then test for the ‘/’ character then get the strings of value1-3. By the way, when I’m inputting these into the file, I am including the ‘\n’ character so you could test for that to test for the newline.

  • 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-14T06:21:06+00:00Added an answer on May 14, 2026 at 6:21 am

    The best method is to read the data per line into a buffer, then parse the buffer. This can be expanded to reading in large blocks of data.

    Use fgets for reading the data into a buffer.

    Use strchr to find the separator character.

    Example:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_TEXT_LINE_LENGTH 128
    
    
    int main(void)
    {
        FILE *  my_file("data.txt", "r");
        char    text_read[MAX_TEXT_LINE_LENGTH];
        char    key_text[64];
        char    value_text[64];
    
        if (!my_file)
        {
            fprintf(stderr, "Error opening data file:  data.txt");
            return EXIT_FAILURE;
        }
        while (fgets(text_read, MAX_TEXT_LINE_LENGTH, my_file))
        {
            char * p;
            //----------------------------------------------
            //  Find the separator.
            //----------------------------------------------
            p = strchr('/');
    
            key_text[0] = '\0';
            value_text[0] = '\0';
    
            if (p != 0)
            {
                size_t  key_length = 0;
                key_length = p - text_read;
    
                //  Skip over the separator
                ++p;
                strcpy(value_text, p);
    
                strncpy(key_text, text_read, key_length);
                key_text[key_length] = '\0';
    
                fprintf(stdout,
                        "Found, key: \"%s\", value: \"%s\"\n",
                        key_text,
                        value_text);
            }
            else
            {
                fprintf(stdout,
                        "Invalid formatted text: \"%s\"\n",
                        text_read);
            }
        } // End:  while fgets
        fclose(my_file);
    
        return EXIT_SUCCESS;
    }
    

    Note: The above code has not been compiled nor tested, but is for illustrative purposes only.

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

Sidebar

Related Questions

Let's say I have an input text file of the following format: Section1 Heading
Let's say I have a file whose format is basic XML, like so: <?xml
Let's say I have a custom file archive format similar to Zip or Rar,
Say I have a file of the following format: 3 4 1,2,3,4 5,6,7,8 9,10,11,12
Say I have file A, in middle of which have a tag string #INSERT_HERE#.
Say I have a file in my kohana 3 website called assets/somefile.jpg . I
So say you have a file that is written in XML or soe other
Say I have a binary file of 12GB and I want to slice 8GB
Say I have a data file that I want to process; I want to
Say I have a htaccess file shared by dev.server and server.site.com. The first domain

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.