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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:28:53+00:00 2026-06-08T19:28:53+00:00

I debugged a function and it is working. So yay, teaching myself C seems

  • 0

I debugged a function and it is working. So yay, teaching myself C seems to be going along well. But I want to make it better. That is, it reads a file like this:

want 
to 
program
better

And puts each individual line of string into an array of strings. Things get weird when I print things out however. As far as I read, strcpy() should just copy a string until the \0 character. If that is true, why is the following printing the string want and \n? It is like strcpy() also copied \n and it is hanging in there. I want to get rid of that.

My code for copying the file is below. I didn’t include the entire program because I don’t believe that is relevant for what is happening. I know the problem is in here.

void readFile(char *array[5049]) 
{
    char line[256]; //This is to to grab each string in the file and put it in a line. 
    int z = 0; //Indice for the array

    FILE *file;
    file = fopen("words.txt","r");

    //Check to make sure file can open 
    if(file == NULL)
    {
        printf("Error: File does not open.");
        exit(1);
    }
    //Otherwise, read file into array  
    else
    {
        while(!feof(file))//The file will loop until end of file
        {
           if((fgets(line,256,file))!= NULL)//If the line isn't empty
           {
             array[z] = malloc(strlen(line) + 1);
             strcpy(array[z],line);
             z++;
           }    
        }
    }
    fclose(file);
}

So now, when I do the following:

     int randomNum = rand() % 5049 + 1;

     char *ranWord = words[randomNum];
     int size = strlen(ranWord) - 1; 
     printf("%s",ranWord);
     printf("%d\n",size);
     int i; 
     for(i = 0; i < size; i++)
     {
          printf("%c\n", ranWord[i]);
     }

It prints out:

these 
6
t
h
e
s
e

Shouldn’t it be printing out the following instead?

 these6
 t
 h
 e
 s
 e

So the only thing I can figure is that when I put the strings into an array, it put the \n in there too. How can I get rid of that?

As always, with respect.
GeekyOmega

  • 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-08T19:28:55+00:00Added an answer on June 8, 2026 at 7:28 pm

    fgets reads the \n in also, it is part of your input file. If you want to get rid of it, do something like:

    int len = strlen(line);
    if (len > 0 && line[len-1] == '\n') line[len-1] = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery function that should make a calculation based on a textbox
UPDATE -- working on getting WAMP with phpDeveloper/Xdebug going. I still want NetBeans --
I debugged that all and noticed, that validator constructor is called (and not one
I've debugged my program and the arrays seem to be allocated well. However for
So I'm trying to implement a recursive function that generates the entire game tree
I've successfully made several Visual Studio debugger visualizers, and they're working very well, except
I have a working drupal batch process. In it I want to copy image
Recently my co-worker showed me a block of code that was not working correctly:
I am working on some PHP classes that can do some basic profiling, debugging,
I am working on a system that uses a Voltage Controlled Oscillator chip (VCO)

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.