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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:26:22+00:00 2026-06-19T02:26:22+00:00

The array I’m working with is: char arr[91][12]; So I populate my array from

  • 0

The array I’m working with is:

char arr[91][12];

So I populate my array from a file using a for loop like so:

for(i = 0; fgets(arr[i], 12, inFile); i++){}

When I want print anything from that array, it’s automatically escaping to the next line.

to test that array:

for(i = 0; fgets(arr[i], 12, inFile); i++){
    printf("%s", arr[i]);
}

//one
//two
//three etc.

//want it to be 'one two three etc.'

I’m trying to use strpbrk() to find \n for each string in the array and change it to \0 like this:

for(i = 0; fgets(arr[i], 12, inFile); i++){
if(strpbrk(arr[i], "\n") != NULL ){
    arr[i][strpbrk(arr[i], "\n")] = '\0';
    }
printf("%s", arr[i]);
}

but this gives me errors. Is there a better way to do what I’m trying to do?

  • 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-06-19T02:26:23+00:00Added an answer on June 19, 2026 at 2:26 am

    It’s actually much simpler than you seem to think. If the newline is at the end (as it is when reading with fgets) just change the last character to the string terminator:

    arr[i][strlen(arr[i]) - 1] = '\0';
    

    The -1 is because arrays (like strings can be considered to be) starts their indexing on zero and goes to length - 1.


    To make sure you really have a newline, you can use e.g. strpbrk like you do (but of course not in the way you do it as explained in other answers). But I think it would be a better idea to use strrchr. First because it’s simpler (only searching for character not string), second because it starts searching from the end:

    char *newlineptr = strrchr(arr[i], '\n');
    if (newlineptr != NULL)
        *newlineptr = '\0';
    

    Or the fastest way of all:

    size_t length = strlen(arr[i]);
    if (arr[i][length - 1] == '\n')
        arr[i][length - 1] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Array { [0] => http://abc.com/video/ghgh23; [1] => http://smtech.com/file/mwerq2; } I want to replace the
Array starts like this: Array ( [SMART Board] => Array ( [0] => sb1
Array #1 looks something like this (simplified): Array ( [Appetizers] => Array ( [0]
array ref.length = 7 (0 - 6), and I want to try to match
Array $ALLOWED_CALLS contains a function name and required parameters. I'd like to filter $_REQUEST
Array output from multiple select option Array ( [q] => Array ( [0] =>
array one: 1,3,5,7 array two: 2,4,6,8 the array i want would be 1,2,3,4,5,6,7,8 I'm
Array ( [inline_comment] => inline_comment [page] => page [comment] => Comment ) I want
Array A looks like this: [1, -inf, 2, 3, inf, -60.2] Array B should
array_pop() returns the last value of an array and removes that value from the

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.