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 6834233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:05:11+00:00 2026-05-26T23:05:11+00:00

I am looking through a data file containing both words and numbers, and I

  • 0

I am looking through a data file containing both words and numbers, and I need to take the numbers out of each row and store them in an array.

Cheryl 2 1 0 1 2 0  
Neal 0 0 2 0 2 0  
Henry 0 2 2 0 2 0  
Lisa 0 0 0 0 2 1

This is how the file is formatted. I start by inputting each line into an array, participants[], and then I need to take the numbers from each line and put them in a separate, two-dimensional array, individually. i.e. the first line would be translated to:

responses[0][0] = 2
responses[0][1] = 1
responses[0][2] = 0
responses[0][3] = 1
responses[0][4] = 2
responses[0][5] = 0

At this point, I detect the first space with char *pointer = strstr(" ", participants[]);, and am able to locate the the beginning of the numbers with strcpy(temp, pointer+1);.
From that point, I run into problems as I am having a hard time translating the string of numbers (still a char string), into individual integer values to be stored in my responses[][] array.

Thanks!

  • 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-26T23:05:11+00:00Added an answer on May 26, 2026 at 11:05 pm

    If that’s you’re exact format, then perhaps fscanf/sscanf could do the job for you? ie.

    #include <stdio.h>
    
    int main(void)
    {
       int nums[6]; 
       char line[] = "Cheryl 2 1 0 1 2 0";
    
       sscanf(line, "%*s %d %d %d %d %d %d", &nums[0], &nums[1], &nums[2], &nums[3], &nums[4], &nums[5]);
    
       printf("%d %d %d %d %d %d\n", nums[0], nums[1], nums[2], nums[3], nums[4], nums[5]);
    
       return 0;
    }
    

    Output:

    2 1 0 1 2 0
    

    And then just check the return value to make sure the right amount of numbers were read.

    EDIT: For an arbitrary amount:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *strchr_ignore_start(const char *str, int c)
    {
       if (c == '\0') return strchr(str, c); /* just to be pedantic */
    
       /* ignore c from the start of the string until there's a different char */
       while (*str == c) str++;
    
       return strchr(str, c);
    }
    
    size_t extractNums(char *line, unsigned *nums, size_t num_count)
    {
       size_t i, count;
       char *endptr = line;
    
       for (i = 0, count = 0; i < num_count; i++, count++) {
          nums[i] = strtoul(line, &endptr, 10);
          if (line == endptr) /* no digits, strtol failed */
             break;
    
          line = endptr;
       }
    
       return count;
    }
    
    int main(void)
    {
       unsigned i, nums[9];
       char line[] = "Cheryl 2 1 0 1 2 0 0 1 2";
       char *without_name;
    
       /* point to the first space AFTER the name (we don't want ones before) */  
       without_name = strchr_ignore_start(line, ' ');
    
       printf("%u numbers were successfully read\n",
              (unsigned)extractNums(without_name, nums, 9));
    
       for (i = 0; i < 9; i++)
          printf("%d ", nums[i]);
    
       putchar('\n');
    
       return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just looking through some information about Google's protocol buffers data interchange format.
I'm getting started with Astoria/ADO.NET Data Services/WCF Data Services. Looking through a lot of
We are looking to run a large amount of data through some unit tests.
i was looking through the django documentation on how to export my data to
I have an application that is looking through some files for old data. In
I'm creating a robots.txt file for my website, but looking through my project structure,
I am looking for optimal approach to use file system based data storage in
I have been looking through Adobe's File Format Specification for PSD (Photoshop files) and
I'm going through an XML file of articles and the journalist(s) that wrote them.
I am looking to iterate through these data structures (basically a directory structure) which

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.