I am having problems with a program that is supposed to take a list of data from an external .txt file, which the user inputs the name of the file, creates a linked list, and then outputs the data to another external .txt file, also which the user inputs the file name. Can anyone help me?
//************************************
//* IMPLEMENTATION FILE (Retail.cpp) *
//************************************
#include "stdafx.h"
#include "PetData.h"
PetData* HeadPointer;
//***************
//* Constructor *
//***************
PetData::PetData()
{
IDNumber = 0;
PetType[25] = '\0';
NextRecordPointer = NULL;
}
//************************************
//* Write the contents of the linked *
//* list to an external report file *
//************************************
void PetData::OutputItem( char* OutputFile )
{
ofstream FileOut;
PetData* CurrentRecordPointer;
FileOut.open((char*)(void*)Marshal::StringToHGlobalAnsi(String::Concat(Directory::GetCurrentDirectory(),
(OutputFile[50]))));
//**********************************************************
//* Records are listed until the NULL value is encountered *
//**********************************************************
CurrentRecordPointer = HeadPointer;
while( CurrentRecordPointer != NULL )
{
FileOut << right;
FileOut << endl << setw(5) << setfill(' ')
<< CurrentRecordPointer->GetIDNumber( );
FileOut << setw(10) << setfill(' ')
<< CurrentRecordPointer->GetPetType( );
CurrentRecordPointer = CurrentRecordPointer->GetLink( );
}
FileOut.close( );
return;
}
void PetData::InsertItem(int InitIDNumber, char InitPetType[], PetData* CurrentRecordPointer )
{
ifstream DataFile;
//*************************************
//* Read first line of data *
//* Input Text File (Priming Reading) *
//*************************************
DataFile >> InitIDNumber;
DataFile.ignore( );
DataFile.get( InitPetType, 7 );
while( DataFile )
{
CurrentRecordPointer = new PetData;
CurrentRecordPointer->InsertItem( InitIDNumber, InitPetType, CurrentRecordPointer );
HeadPointer = CurrentRecordPointer;
//***********************************************
//* Read next line of data from input text file *
//***********************************************
DataFile >> InitIDNumber;
DataFile.ignore( );
DataFile.get( InitPetType, 7 );
}
}
//*************************************************
//* Return the value of the private class members *
//*************************************************
int PetData::GetIDNumber( ) const
{
return( IDNumber );
}
char* PetData::GetPetType( ) const
{
return( PetType );
}
PetData* PetData::GetLink( ) const
{
return NextRecordPointer;
}
//**********************************************
//* Set the value of the private class members *
//**********************************************
void PetData::SetIDNumber( int InitIDNumber )
{
IDNumber = InitIDNumber;
}
void PetData::SetPetType( char* InitPetType )
{
strcpy_s( PetType, InitPetType );
}
void PetData::SetLink( PetData* InitLink )
{
NextRecordPointer = InitLink;
}
The stdafx.h file:
1 2 3 4 5 6 7 8 9
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
#pragma once
// TODO: reference additional headers your program requires here
#include "PetData.h"
extern PetData* HeadPointer;
The FinalProject.cpp file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// FinalProject.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
usingnamespace FinalProject;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
I'm having issues getting the output file to output the data from the linked list. When I get the program to use the names of the input and output files, I'm not getting the data in the output file. The code was not written for me.
You need to pick a format for the file that is easy to read and easy to write. I suggest one line for each record with the ID first, followed by type. You can read it like this:
1 2 3 4
// is some istream:
is >> IDNumber;
getline(is, str);
strcpy(PetType, str.c_str());
and read it like this:
1 2
// os is some ostream
os << IDNumber << ' ' << PetType << '\n';
Next, you've made a common mistake of combining (1) the list itself and (2) the items within the list. You should have two separate classes for these.
Each function should do just one thing. Consider InsertItem. What you had to read the input from the terminal instead of a file? What if you had to insert an item that was read from a database? You'd have to refactor your code to accomplish either of these. That's why you should break it down:
- a method to read a PetData from an istream
- a separate method to insert a PetData into the list.
- The caller should create an ifstream for the input file and then use the method to read the list.