I need to create a program that uses a structure that alocates memory with the new operator. The information must be saved in a file, then displayed. The problem I have is that the program saves only the first character of the words I enter. Could someone please help me make it work.

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <new>
using namespace std;

    FILE *f;
    struct dog {
        char  name;
        char  family;
        int   age;

                }list1, list2;

    int i,n,optie;


//File Dog 
int file_dog()
{   
    dog* plist; 

    cout << "  Point the number of dogs you want to enter\n" << endl;
    cin >> n;   

    plist = new (nothrow) dog[n];
    f=fopen("Dog.txt","w");
    if (plist == 0)
    cout << "Error: memory could not be allocated";
    else
    {
    for(i=0;i<n;i++)
     {
      cout << "  Enter the name of the dog       - " << endl;
      cin >> list1.name;
       fflush(stdin);
      cout << "  Enter the family of the dog     - "  << endl;
      cin >> list1.family;
       fflush(stdin);
      cout << "  Enter the age                   - " << endl;
      cin >> list1.age;
      fwrite(&list1,sizeof(list1),1,f);
      cout << endl;
     }
    cout << endl;
    fclose(f);
    delete []plist;
    }
    return 0;
}


int display_the_file_dog()
{   

    if((f=fopen("Dog.txt","r")) == NULL )
       {
         cout << "Error" << endl;
         exit(1);
       }

    cout << "          Date despre ciini" << endl;
    cout << "  ===================================" << endl;
    cout << "  | Nr |  Name |  Family  | Age | " << endl;
    cout << "  ===================================" << endl;
    i=1;
    fread(&list2,sizeof(list2),1,f);
    while(!feof(f))
    {
     cout << "    " << i++ << "       " << list2.name << "        " << list2.family << "        " << list2.age << endl;
     fread(&list2,sizeof(list2),1,f);
    cout << "  -----------------------------------" << endl;
    }
    cout << endl;
    fclose(f);
    return 0;
}


int main()
{   
    system("color 0");
    system("color f0");

    while (1)
     {
      cout << "                      -------------- M E N I U -------------" << endl;
      cout << "                      ----------- Choose an option ---------" << endl;
      cout << endl;
      cout << "                      | [1] - Create the file Dog          |" << endl;
      cout << "                      | [2] - Display the file Dog         |" << endl;
      cout << "                      | [3] - Exit the program             |" << endl;
      cout << "                      | ---------------------------------- |" << endl;
      cin >> optie; fflush(stdin);
    switch (optie)
     {
      case 1: file_dog(); break;
      case 2: display_the_file_dog(); break;
      case 3: exit(1);
      default: printf("Choose the right option\n"); break;
     } 
    }
    return 0;
}

Dani AI

Generated

is right — the fields used to hold the name and family are only single characters, so you’ll only ever see the first letter. There are a few other issues in the posted code worth fixing at the same time: the dynamically allocated array is never used (the code fills and writes list1 instead of plist[i]), fflush(stdin) is undefined behavior, and using fwrite/fread with C++ objects that contain non-trivial members (like std::string) will not work correctly.

Two simple, safe options:

  • Use fixed-size C strings in the struct (char arrays). Then fwrite/fread of the struct is OK for local, same-platform storage.
  • Use std::string and write/read as text (or serialize each string with length) — do not fwrite the raw struct.

Example using fixed-size arrays (minimal, focused on the core fixes):

struct dog {
  char name[64];
  char family[64];
  int age;
};

// write
dog* plist = new(std::nothrow) dog[n];
// ... read input into plist[i].name / plist[i].family (use cin.getline or cin >> then cin.ignore)
FILE* f = fopen("Dog.dat","wb");
fwrite(plist, sizeof(dog), n, f);
fclose(f);
delete [] plist;

// read back
FILE* fr = fopen("Dog.dat","rb");
dog temp;
while (fread(&temp, sizeof temp, 1, fr) == 1) {
  cout << temp.name << " " << temp.family << " " << temp.age << '\n';
}
fclose(fr);

Quick tips:

  • Open binary files with "wb"/"rb" when using fwrite/fread.
  • Replace fflush(stdin) with cin.ignore(...) and use cin.getline() for names with spaces.
  • If you switch to std::string, write a text format (one record per line or length-prefixed fields) or use std::ofstream/std::ifstream with proper parsing.
  • Avoid while (!feof(f)); check fread’s return value instead.

These changes fix the “only first character” symptom and make the file I/O reliable and portable.

char name;
char family;
int age;

Could it be that your name is only 1 character long. If you want a full name, consider the use of a string. Same goes for the family.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.