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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:00:18+00:00 2026-05-27T00:00:18+00:00

#include <stdio.h> #pragma pack(push) #pragma (1) typedef struct contact { char firstname [40]; char

  • 0
#include <stdio.h>

#pragma pack(push)
#pragma (1)

typedef struct contact {    
    char firstname [40];    
    char lastname [40]; 
    char address [100]; 
    char phone[10];
}contact;

#pragma pack(pop)

int main ()
{   FILE *pFile;
    contact entry = {"", "", "", ""};
    char choice;

    pFile = fopen("C:\\contacts.txt", "w+");

if(!pFile){
    printf("File could not be open");
    return 1;
    }

printf("Choose a selection\n\n");
printf("1. Enter First Name\n");
printf("2. Enter Last Name\n");
printf("3. Enter Address\n");
printf("4. Enter Phone Number\n\n");
scanf( "%d", &choice);

switch (choice){

     case 1:
        printf("First name: \n");     
        fgets(entry.firstname, sizeof(entry.firstname),pFile);
        break;
     case 2:
        printf("Last name: \n");      
        fgets(entry.lastname, sizeof(entry.lastname),pFile);
        break;
     case 3:
        printf("Address: \n");    
        fgets(entry.address, sizeof(entry.address),pFile);
        break;
     case 4:
        printf("Phone Number: \n");   
        fgets(entry.phone, sizeof(entry.phone),pFile);
        break;
     default:
        printf(" No Choice selected, Ending Address Book Entry system");
        break;
    }
        fwrite(&entry, sizeof(contact), 1, pFile);

        printf("Enter a new contact?");
        scanf("%s", &choice);

     //while(choice != 'n');

fclose(pFile);

getchar();
return 0;
}

This code after I choose an entry and after I put an entry in and press enter it crashes saying Stack around the variable ‘entry’ was corrupted. I’m fairly confident that it is my fwrite function that I’m using. I know the first parameter fwrite looks for is a pointer to an array of elements to be written but I think I’m just confused right now. Any help would be much appreciated.

  • 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-27T00:00:19+00:00Added an answer on May 27, 2026 at 12:00 am

    You should change all your

    fgets(entry.firstname, sizeof(entry.firstname),pFile);
    

    to

    fgets(entry.firstname, sizeof(entry.firstname),stdin);
    

    Because you’re reading from the console, not the file.

    Also, with

    scanf("%s", &choice);
    

    and

    scanf( "%d", &choice);
    

    you’re trying to read a string and a digit and store it in a char. They both should be

    scanf("%c", &choice);
    

    That said, you should think about rewriting this using ifstream, cin, getline, and std::string to make your life easier if you’re not looking for maximum performance.

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

Sidebar

Related Questions

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#pragma inspect, symbols #pragma nolist #pragma ENV LIBSPACE #include <stdio.h> #include <string.h> #include <tal.h>
#include <winsock2.h> #include <windows.h> #include<stdio.h> #pragma comment(lib, Ws2_32.lib) #include <iostream> using namespace std; int
To clarify my question, let's start off with an example program: #include <stdio.h> #pragma
When I do: less /usr/include/stdio.h (which is only a C library - nothing to
I am going to paste here my code and errors: #include stdio.h #include winsock2.h
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<time.h> int main() { clock_t start; double d; long int n,i,j; scanf(%ld,&n); n=100000;
#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }

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.