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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:28:03+00:00 2026-05-30T16:28:03+00:00

I am reading the contents of a file into a 2D array. The file

  • 0

I am reading the contents of a file into a 2D array. The file is of the type:

FirstName,Surname
FirstName,Surname

etc. This is a homework exercise, and we can assume that everyone has a first name and a surname.

How would I go about splitting the line using the comma so that in a 2D array it would look like this:

char name[100][2];

with

       Column1     Column2
Row 0  FirstName   Surname
Row 1  FirstName   Surname

I am really struggling with this and couldn’t find any help that I could understand.

  • 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-30T16:28:05+00:00Added an answer on May 30, 2026 at 4:28 pm

    You can use strtok to tokenize your string based on a delimiter, and then strcpy the pointer to the token returned into your name array.

    Alternatively, you could use strchr to find the location of the comma, and then use memcpy to copy the parts of the string before and after this point into your name array. This way will also preserve your initial string and not mangle it the way strtok would. It’ll also be more thread-safe than using strtok.

    Note: a thread-safe alternative to strtok is strtok_r, however that’s declared as part of the POSIX standard. If that function’s not available to you there may be a similar one defined for your environment.

    EDIT: Another way is by using sscanf, however you won’t be able to use the %s format specifier for the first string, you’d instead have to use a specifier with a set of characters to not match against (','). Since it’s homework (and really simply) I’ll let you figure that out.

    EDIT2: Also, your array should be char name[2][100] for an array of two strings, each of 100 chars in size. Otherwise, with the way you have it, you’ll have an array of 100 strings, each of 2 chars in size.

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

Sidebar

Related Questions

I am reading the contents of the file using fread into an char array.
I'm reading the contents of file into a 9 element array. I need to
I'm reading a file into a byte array in chunks and sending it over
i am trying to read jpg file and convert into byte array this is
I have a string from reading a .txt file that looks something like this:
I am reading in a text file using FileInputStream that puts the file contents
I am reading a file into byte array, i have inserted some control command
I have the following simple code, that reads contents of a text file into
I'm reading an xml file using Jaxb, now the contents of the xml file
I have an image gallery that I created by reading the contents inside a

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.