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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:04:06+00:00 2026-05-28T22:04:06+00:00

How do I parse out the fields in a comma separated string into a

  • 0

How do I parse out the fields in a comma separated string into a integer array in C?

Input file date is like below-

6
1,2,77
1,5,32
1,3,54
2,4,12
4,5,52
4,3,56
6,2,8
6,5,30
3,6,44

I need those data will be stored in a array[u][v]. u = 1st column, v = 2nd column. and value of this array will be 3rd item of each row. Please help me out

  • 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-28T22:04:06+00:00Added an answer on May 28, 2026 at 10:04 pm

    Am I right that the first line gives the size of the array? I take this as an assumption.

    The parsing of the string is done with scanf() in C and the stream extractor operator in C++. It requires a bit of care to handle the commas and newlines correctly.

    The next problem I see is not directly related to the “parsing” problem, it is the handling of the integer array. Do you want that dynamic or static? (static means: the size of the array is known at compile time). If you want to have it dynamic you cannot use constructs like this: array[u][v] = value; in C. In C++ you can, but you need to declare the array correctly. Both of my solutions below use dynamic arrays.

    C

    This is a solution in pure C.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
      int size, u, v, value;
      FILE * pFile;
      int *array;
    
      pFile = fopen ("data.txt","r");
    
      fscanf (pFile, "%d", &size);   // reading the first line here
    
      array = malloc(size*size*sizeof(int)); // allocate the array based on size of first line
      memset(array,0,size*size*sizeof(int)); // set the array to zero
    
      while(!feof(pFile))       // read until the end...
      {
        fscanf(pFile, "%d,%d,%d\n", &u, &v, &value); // read 3 integer values
        array[(u-1)*size + v] = value;  // we have to calculate the index ourselves
      }
      fclose(pFile);    // we are done with reading
    
      // the follwing prints the contents of the array to visualize
      for(u = 1; u<=size; ++u)
      {
        for(v=1; v<=size; ++v)
          printf("%d, ", array[(u-1)*size+v]);
        printf("\n");
      }
    
      free(array);  //
      return 0;
    }
    

    C++

    here the same thing in C++. However, it does not use STL containers for the dynamic array, which could be more “elegant”.

    #include <fstream>
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
      char c;
      int size, u,v,value;
    
      fstream file("data.txt");
      file >> size;
    
      int array[size+1][size+1] ;
      memset(array,0,sizeof(array));
    
      while(file.good())
      {
        file >> skipws >> u >> c >> v >> c >> value;
        array[u][v] = value;
      }
    
      for(u = 1; u<=size; ++u)
      {
        for(v=1; v<=size; ++v)
          cout <<  array[u][v] << ", ";
        cout << "\n";
      }
      file.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to parse out the string that has following structure x:{a,b,c,}, y:{d,e,f} etc.
I found this function to parse a csv string into a multi-dim array in
I want to parse out each modified file that is reported during FCIV's verification
Trying to parse an SQL string and pull out the parameters. Ex: select *
Hi I want to parse a bibtex publications file and sort for specific fields
I would like to parse a CSV file so that each row is treated
Ok I have to parse out a SOAP request and in the request some
Hi I'm using ElementTree to parse out an xml feed from Kuler. I'm only
I'm trying to write a regex that will parse out the directory and filename
Can Spirit (part of Boost C++ library) be used to parse out binary data

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.