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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:14:10+00:00 2026-05-27T20:14:10+00:00

I am writing a program to read stdin and output it. int main() {

  • 0

I am writing a program to read stdin and output it.

int main() {
    FILE* inputF = stdin;
    char* inputStr[10];

    fread(inputStr, 1, 9, inputF);
    if(ferror(inputF)) {
        printf("An error occurred");
        return 0;
    }

    inputStr[9] = '\0';

    printf("%s", (const char*)inputStr);

    return 0;
}

It should create a 10 character long string and read 9 bytes of stdin into it, and then put '\0' in position 9.

When I run the program, this is the result:

gab@testvm:~/work/c/fibo$ ./a.out < test.txt
56                                                                                                     `ô
                                                                                                       ga

Two lines and excess characters are printed (scroll to the right to see them).

What might be causing this?

  • 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-27T20:14:11+00:00Added an answer on May 27, 2026 at 8:14 pm

    @DanielFischer has pointed out your immediate problem, however once you’ve fixed the char[] vs char*[] issue, you should also note…

    If test.txt contains fewer than 9 characters then the you will still have junk characters between the last read character and the position where your write the null terminator.

    You should check the return value of fread to see how many characters were read and write the null terminator in the appropriate place.

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

Sidebar

Related Questions

Im writing a program that should read input via stdin, so I have the
I'm writing a program for an exercise that will read data from a file
I'm writing a program to read data from a file, which may be in
I am writing my first clojure program, and want to read lines from stdin.
I'm writing a program to read from a POP3 mailbox and upload the email
I am writing a program to read files and search text in it. I
I'm writing a program in Java where I read in data from an XML
I'm writing a C++ program which needs to be able to read a complex
I am writing a small computation program with lot of read operations on blob
In a program that I'm writing, I wanted to make a ConfigParser that's read

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.