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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:03:14+00:00 2026-06-16T05:03:14+00:00

I use VS 2010 as my IDE and this code works fine until the

  • 0

I use VS 2010 as my IDE and this code works fine until the line where fgets is called as a puts argument. It writes down the numbers in the file fine but it also prints some annoying gibberish. Maybe I am missing a \0 somewhere, dunno. Other people tried it on other compilers like mingw or gcc and it works fine.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *a, n, i;
    char str[512];
    FILE *f;
    printf("Insert array size: ");
    scanf("%d", &n);
    if(n <= 0)
    {
        printf("%d is not an allowed value!\n", n);
        return 1;
    }
    a = (int*)malloc(n * sizeof(int));
    if(a == NULL)
        return 2;
    putchar('\n');
    f = fopen("myarray.txt", "r+");
    if(f == NULL)
        return 3;
    for(i = 0; i < n; ++i)
    {
        printf("Insert %d. element of the array: ", i + 1);
        scanf("%d", &a[i]);
        fprintf(f, "%d ", a[i]);
    }
    putchar('\n');
    puts(fgets(str, 512, f));
    free(a);
    fclose(f);
    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-16T05:03:15+00:00Added an answer on June 16, 2026 at 5:03 am

    At the point in your code where you call fgets your file pointer should be at the end of the file.

    Thus, fgets should be returning you the NULL pointer, since it hits EOF before reading any characters.

    Thus, you’re passing the NULL pointer to puts.

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

Sidebar

Related Questions

this code works fine: procedure TForm2.Timer1Timer(Sender: TObject); var Text: string; begin SetLength (Text,555); GetWindowText
I use VS 2010 to debug a single IronPython module. Everything works great. I
Windows 7 64 SP1, Boost 1.42, MS VS 2010 Ultimate, C++ This stripped down
Is it possible to use de Delphi 7 compiler inside Delphi 2010 IDE? We
I only use one IDE (Visual Studio 2010), and preferred to stick with one
I currently use VS 2010 Express for my C++ IDE needs, but have found
When attempting to use the profiler in Visual Studio Ultimate 2010, the IDE throws
I’m a fan of the new VS 2010 Web.config transformations. I use this feature
I am using Visual Studio 2010. Using what, the text editor of this IDE
I use VisualStudio 2010 and olso have Visual C# 2010 Express installed on my

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.