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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:07:47+00:00 2026-06-12T09:07:47+00:00

I need to read in a file from C, store it in an array

  • 0

I need to read in a file from C, store it in an array and print its contents. For some reason I keep seeing octal in my output near the end. I am dynamically creating the array after counting how many lines and characters are in it after opening the file.

output:

Abies 
abies 
abietate 
abietene 
abietic 
abietin
\320ѿ_\377Abietineae --> umlaut? where did he come from?
y\300_\377abietineous

code:

int main(int argc, char ** argv) {

char c = '\0';
FILE * file;
int i = 0, j = 0, max_line = 0, max_char_per_line = 0;

/* get array limits */
file = fopen(argv[1], "r");
while ((c = fgetc(file)) != EOF){
    if (c == '\n'){
        max_line++; j++;
        if (j > max_char_per_line){
            max_char_per_line = j;
        }
        j = 0;
        continue;
    }
    j++;
}

rewind(file);
/* declare array dynamically based on max line and max char */
char word[max_line][max_char_per_line];

/*read in file*/
j = 0; c = '\0';

while ((c = fgetc(file)) != EOF){
    if (c == '\n'){
        word[i][j] = '\0';
        i++; j=0;
        continue;
    }

    word[i][j] = c;
    j++;
}
word[i][j] = '\0';

fclose(file);
for (i = 0; i < max_line; i++){
    printf("%s\n", word[i]);
}



    return 0;
}
  • 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-12T09:07:47+00:00Added an answer on June 12, 2026 at 9:07 am

    Change read routine:

       if (c == '\n'){
            word[i][j] = 0x0;
            i++; j=0;
            continue;
        }
    

    and add the “\n” back in the printf routine.

    for (i = 0; i < max_line; i++){
        printf("%s\n", word[i]);
    }
    

    C strings are zero-terminated, not “\n”-terminated, so when you printf()ed them, printf() did not know where to stop printing.

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

Sidebar

Related Questions

I want to read a text file and store its contents in an array
I need to read some data from an input file and plot a graph
I need to read from either an array or a file (preferably a file
I'm trying to read some numbers (double) from a file and store them in
I need to read a file from the local file system using JSP, save
I need to read char[] (size is COUNT) from text file from OFFSET with
I need to read a BufferedImage from file, which doesn't use DataBufferInt (as normally),
I need to read in an expression from a file using a string stream
I need to read the data from a file that can be either comma
I need to read 16 bits from the binary file as std::string or char

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.