This is my code for a database, whenever i save the database to the file 'database.txt' and display the results i get this;
name - (correct, is what i put in)
pin - 3435973836
slew rate - -9.25596e+061
these values are the same for every entry i add, i cant seem fo figure out why.
Plus, the text file i save to is supposed to have the format of
length
Name
Pin
Slew
Name
Pin Slew
However, the text file looks nothing like it, any ideas as to why it isnt in this format??
code; -
#include <stdafx.h>
-
#include <iostream>
-
#include <fstream>
-
#include <string.h>
-
using namespace std;
-
-
struct OpAmps {
-
char Name[20]; // the name of the op-amp (e.g. "741")
-
unsigned int PinCount; // the number of pins in the package
-
double SlewRate; // the slew rate in volts per microsecond
-
};
-
-
#define DATABASE_MAX 10
-
-
#define DATABASE_FILENAME "database.txt"
-
-
// function prototypes
-
-
void Enter(OpAmps&, unsigned long&);
-
void Save(const OpAmps*, const unsigned long&);
-
void Load(OpAmps*, unsigned long&);
-
void Sort(OpAmps*, unsigned long&);
-
void Display(OpAmps*, const unsigned long&);
-
-
int SortByName(const void*, const void*);
-
int SortBySlewRate(const void*, const void*);
-
-
int main()
-
{
-
OpAmps OpAmp[DATABASE_MAX]; // the database
-
unsigned long database_length = 0; // the number of elements in the database
-
char UserInput;
-
-
// loop until the user wishes to exit
-
while (1) {
-
-
// show the menu of options
-
cout << endl;
-
cout << "Op-amp database menu" << endl;
-
cout << "--------------------" << endl;
-
cout << "1. Enter a new op-amp into the database" << endl;
-
cout << "2. Save the database to disk" << endl;
-
cout << "3. Load the database from disk" << endl;
-
cout << "4. Sort the database" << endl;
-
cout << "5. Display the database" << endl;
-
cout << "6. Exit from the program" << endl << endl;
-
-
// get the user's choice
-
cout << "Enter your option: ";
-
cin >> UserInput;
-
cout << endl;
-
-
// act on the user's input
-
switch(UserInput) {
-
case '1':
-
Enter(OpAmp[database_length], database_length);
-
break;
-
-
case '2':
-
Save(OpAmp, database_length);
-
break;
-
-
case '3':
-
Load(OpAmp, database_length);
-
break;
-
-
case '4':
-
Sort(OpAmp, database_length);
-
break;
-
-
case '5':
-
Display(OpAmp, database_length);
-
break;
-
-
case '6':
-
return 0;
-
-
default:
-
cout << "Invalid entry" << endl << endl;
-
break;
-
}
-
}
-
}
-
-
-
void Enter(OpAmps& OpAmp, unsigned long& length)
-
// if the database is full, inform the user
-
{
-
char line[20];
-
unsigned int Pin;
-
double Slew;
-
bool cont=false;
-
-
if (length == 10)
-
{
-
cerr << "The database is full" << endl;
-
}
-
-
// if the database is not full, get the data from the user and alter the database length
-
-
else
-
while (cont==false)
-
{
-
cout << "Enter Operational Amplifier name: ";
-
cin.ignore();
-
cin.getline(line, sizeof(line), '\n');
-
strcpy_s(OpAmp.Name, line);
-
cont=true;
-
}
-
-
-
cont = false;
-
while (cont==false)
-
{
-
cout << endl << "Enter number of pins: ";
-
cin.ignore();
-
cin.getline(line, sizeof(line));
-
scanf_s(line,"%u",&Pin), '\n';
-
OpAmp.PinCount=Pin;
-
cont=true;
-
}
-
-
cont = false;
-
while (cont==false)
-
{
-
cout << endl << "Enter slew rate: ";
-
cin.ignore();
-
cin.getline(line, sizeof(line), '\n');
-
scanf_s(line,"%f",&Slew);
-
OpAmp.SlewRate=Slew;
-
cont=true;
-
}
-
length++;
-
-
cout << endl << "Operator Amplifier in memory, to save select option 2 from main menu" << endl;
-
}
-
-
void Save(const OpAmps* OpAmp, const unsigned long& length)
-
{
-
fstream output_file; // file stream for output
-
-
// open the file
-
//<enter code here>
-
output_file.open(DATABASE_FILENAME, ios::out);
-
if (!output_file.good())
-
{
-
cout << "Could not create file";
-
exit (1);
-
}
-
-
cout << "Opened file to save data to" << endl;
-
-
// write length information to file
-
output_file << length << endl << endl;
-
-
// write data to file
-
unsigned int i;
-
for (i=0;i<length;i++);
-
{
-
output_file.write((char *)(&OpAmp[i]), sizeof(OpAmp));
-
}
-
cout << length << " entry(s) added to database" << endl;
-
// close the file
-
output_file.close();
-
}
-
-
void Load(OpAmps* OpAmp, unsigned long& length)
-
{
-
fstream input_file; // file stream for input
-
-
//open the file
-
input_file.open(DATABASE_FILENAME, ios::in);
-
if (!input_file.good())
-
{
-
cout << "Could not open file";
-
exit (1);
-
}
-
-
// load database length information from file
-
else
-
{
-
cout << "File opened" << endl;
-
-
char templine[20];
-
char emptyline[20];
-
-
OpAmps* pOpAmps = &OpAmp[0];
-
-
input_file >> emptyline;
-
unsigned long tempLn=0;
-
scanf_s(emptyline, "%lu", &tempLn);
-
cout << tempLn << endl;
-
length = tempLn;
-
-
if (length==0)
-
{
-
cout << "Database empty" << endl;
-
}
-
else
-
{
-
unsigned int Pin;
-
float Slew;
-
-
for (unsigned int i=0;i<length;i++)
-
{
-
input_file>>templine;
-
cout<<templine<<endl;
-
strcpy_s((*pOpAmps).Name,templine);
-
-
input_file>>templine;
-
-
if (scanf_s(templine,"%u",&Pin))
-
{
-
(*pOpAmps).PinCount=Pin;
-
cout<<Pin<<endl;
-
}
-
else
-
{
-
cout<<"Error reading "<< ((*pOpAmps).Name) <<endl;
-
}
-
-
input_file>>templine;
-
-
if (scanf_s(templine,"%f",&Slew))
-
{
-
(*pOpAmps).SlewRate=Slew;
-
cout<<Slew<<endl;
-
}
-
else
-
{
-
cout<<"Error reading" << ((*pOpAmps).Name) << endl;
-
}
-
pOpAmps++;
-
}
-
cout<<"Total "<<length<< "entry(s) read"<<endl;
-
}
-
}
-
-
-
-
-
-
// close the file
-
input_file.close();
-
}
-
-
-
void Sort(OpAmps* OpAmp, unsigned long& length)
-
{
-
char UserInput;
-
-
// show the menu of options
-
cout << endl;
-
cout << "Sorting options" << endl;
-
cout << "---------------" << endl;
-
cout << "1. To sort by name" << endl;
-
cout << "2. To sort by slew rate" << endl;
-
cout << "3. No sorting" << endl << endl;
-
-
// get the user's choice of sorting operation required
-
cout << "Enter your option: ";
-
cin >> UserInput;
-
cout << endl;
-
-
// act on the user's input
-
//<enter code here>
-
switch(UserInput)
-
{
-
-
case '1':
-
qsort((void*)&OpAmp[0], length, sizeof(OpAmp[0]), SortByName);
-
break;
-
-
case '2':
-
qsort((void*)&OpAmp[0], length, sizeof(OpAmp[0]), SortBySlewRate);
-
break;
-
-
case '3':
-
-
break;
-
-
default:
-
cout << "Invalid entry" << endl << endl;
-
break;
-
}
-
-
}
-
-
-
int SortByName(const void* Value1, const void* Value2)
-
{
-
//<enter code here>
-
return strcmp((*(OpAmps*)Value1).Name, (*(OpAmps*)Value2).Name);
-
}
-
-
-
int SortBySlewRate(const void* Value1, const void* Value2)
-
{
-
//<enter code here>
-
if ((*(OpAmps*)Value1).SlewRate<(*(OpAmps*)Value2).SlewRate)
-
{
-
return -1;
-
}
-
else if ((*(OpAmps*)Value1).SlewRate>(*(OpAmps*)Value2).SlewRate)
-
{
-
return 1;
-
}
-
else
-
{
-
return 0;
-
}
-
}
-
-
void Display(OpAmps* OpAmp, const unsigned long& length)
-
{
-
OpAmps* pOpAmps;
-
pOpAmps=&OpAmp[0];
-
-
// if the database is empty, inform the user
-
if (length==0)
-
{
-
cout << "The database is empty";
-
}
-
// if the database is not empty, display all the elements in the database
-
//<enter code here>
-
else
-
{
-
for (unsigned int i=0;i<length;i++)
-
{
-
cout << "Operational Amplifier name : " << (*pOpAmps).Name << endl;
-
cout << "Number of pins : " << (*pOpAmps).PinCount << endl;
-
cout << "Slew Rate : " << (*pOpAmps).SlewRate << " Volts/microsecond" << endl << endl;
-
pOpAmps++;
-
}
-
}
-
}
7 2664
The problem is likely with the line - output_file.write((char *)(&OpAmp[i]), sizeof(OpAmp));
inside the for...loop of the Save function. You are casting the first portion to a char*, but the thing you are casting is the address of an OpAmps object. This is probably a bad cast, which is giving you the strange results.
Also, the second argument is using sizeof(OpAmp), which is the size of the array you are using - should it be sizeof(OpAmps), the struct?
you are not reading the pin and slew rate values correctly in function Enter(), try -
void Enter(OpAmps& OpAmp, unsigned long& length)
-
// if the database is full, inform the user
-
{
-
char line[20];
-
unsigned int Pin;
-
double Slew;
-
bool cont=false;
-
-
if (length == 10)
-
{
-
cerr << "The database is full" << endl;
-
}
-
-
// if the database is not full, get the data from the user and alter the database length
-
-
else
-
while (cont==false)
-
{
-
cout << "Enter Operational Amplifier name: ";
-
cin.ignore();
-
cin.getline(line, sizeof(line), '\n');
-
strcpy(OpAmp.Name, line);
-
cont=true;
-
}
-
-
-
cont = false;
-
while (cont==false)
-
{
-
cout << endl << "Enter number of pins: ";
-
//cin.ignore();
-
//cin.getline(line, sizeof(line));
-
scanf("%u",&Pin), '\n';
-
OpAmp.PinCount=Pin;
-
cont=true;
-
}
-
-
cont = false;
-
while (cont==false)
-
{
-
cout << endl << "Enter slew rate: ";
-
//cin.ignore();
-
//cin.getline(line, sizeof(line), '\n');
-
scanf("%lf",&Slew);
-
OpAmp.SlewRate=Slew;
-
cont=true;
-
}
-
length++;
-
cout << OpAmp.Name << " " << OpAmp.PinCount << " " << OpAmp.SlewRate << " " << endl;
-
cout << endl << "Operator Amplifier in memory, to save select option 2 from main menu" << endl;
-
}
-
-
it is a good idea in a function such as enter where you read in data to print it out to screen so you can check the read worked
So in the console that works fine, but in the database.txt file the program creates the information is not stored conrrectly. The file only contains the length and then alot of garbage, cant seem to pinpoint where the code is wrong
ideas??
I have changed a part of the save function to this:
unsigned int i;
for (i=0;i<length;i++);
{
//output_file.write((const char*)&OpAmp[i], sizeof(OpAmps));
output_file << endl;
output_file << OpAmp[i].Name << endl;
output_file << OpAmp[i].PinCount << endl;
output_file << OpAmp[i].SlewRate << endl;
but it is still not saving the correct text to file, all just garbage still. All the online help i have looked at doesnt help me solve the problem...
I have changed a part of the save function to this:
unsigned int i;
for (i=0;i<length;i++);
{
//output_file.write((const char*)&OpAmp[i], sizeof(OpAmps));
output_file << endl;
output_file << OpAmp[i].Name << endl;
output_file << OpAmp[i].PinCount << endl;
output_file << OpAmp[i].SlewRate << endl;
but it is still not saving the correct text to file, all just garbage still. All the online help i have looked at doesnt help me solve the problem...
you have a ; on the end of your for() statement - this will terminate the for and on exit i will = length so garbage is written to the file
thanks, so last thing, in the Load function, i always get a length of zero, it is supposed to read the first line of the text file and then read the corresponding entries, the first line in the file is the number 3, so it should read three, but doesnt, ideas?? - void Load(OpAmps* OpAmp, unsigned long& length)
-
{
-
fstream input_file; // file stream for input
-
-
//open the file
-
-
input_file.open(DATABASE_FILENAME, ios::in);
-
if (!input_file.good())
-
{
-
cout << "Could not open file";
-
exit (1);
-
}
-
-
// load database length information from file
-
-
else
-
{
-
cout << "File opened" << endl;
-
-
// load data from file
-
-
char templine[20];
-
char emptyline[20];
-
-
OpAmps* pOpAmps = &OpAmp[0];
-
-
input_file >> emptyline;
-
unsigned long tempLn=0;
-
if (scanf_s(emptyline, "%lu", &tempLn))
-
{
-
cout << tempLn << endl;
-
length = tempLn;
-
}
-
else
-
{
-
cout << "Error" << endl;
-
}
-
-
if (length==0)
-
{
-
cout << "Database empty" << endl;
-
}
-
else
-
{
-
unsigned int Pin;
-
float Slew;
-
-
for (unsigned int i=0;i<length;i++)
-
{
-
input_file>>templine;
-
cout<<templine<<endl;
-
strcpy_s((*pOpAmps).Name,templine);
-
-
input_file>>templine;
-
-
if (scanf_s(templine,"%u",&Pin))
-
{
-
(*pOpAmps).PinCount=Pin;
-
cout<<Pin<<endl;
-
}
-
else
-
{
-
cout<<"Error reading "<< ((*pOpAmps).Name) <<endl;
-
}
-
-
input_file>>templine;
-
-
if (scanf_s(templine,"%lf",&Slew))
-
{
-
(*pOpAmps).SlewRate=Slew;
-
cout<<Slew<<endl;
-
}
-
else
-
{
-
cout<<"Error reading" << ((*pOpAmps).Name) << endl;
-
}
-
pOpAmps++;
-
}
-
cout<<"Total "<<length<< "entry(s) read"<<endl;
-
}
-
}
Instead of reading the first line into a character array, since you know it will be an int value, why not try reading the value directly into length? Something like:
I only say this because I've never used scanf_s and am not sure if this line is producing errors.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: G-Factor |
last post by:
Hi all
I've just started learning about saving files. I got bit of a problem. The
following code gives me an error about incompatible types. (Cannot covert
from class character to char *). I...
|
by: dale zhang |
last post by:
Hi,
I am trying to save and read an image from MS Access DB based on the
following article:
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp
Right now, I saved images without any...
|
by: Dan DeConinck |
last post by:
Hello,
I want to save the position of all the controls on my form. I would like to
write them out to a preference file.
I have done this in DOS BASIC like this:
TO WRITE the preference...
|
by: TheGanjaMan |
last post by:
Hi everyone,
I'm trying to write up a simple image stamper application that stamps the
Exif date information from the jpegs that I've taken from my digital
camera and saves the new file with the...
|
by: Pedro Leite |
last post by:
Good Afternoon.
the code below is properly retreiving binary data from a database and saving
it. but instead of saving at client machine is saving at the server machine.
what is wrong with my...
|
by: Mark Denardo |
last post by:
My question is similar to one someone posted a few months back, but I don't
see any replies.
Basically I want to be able to have users upload photos and save them in a
database (as byte data)...
|
by: Speilman_54 |
last post by:
Hi,
I'm converting an excel Macro into visual basic 2005 express, as I
don't have a copy of VB 6 and trying to make and executable from it,
I know this version doesn't have the save file as .exe,...
|
by: Allie |
last post by:
Hi, all.
This might be a silly question... but I am very new to programming in
SQL so please bear with me :)
So. I'm using MS SQL Server 2005 Management Studio Express. I have a
table that...
|
by: Karl |
last post by:
Hi all,
It may seem like a rather odd request (or not) but I would like to be
able to create a file (doc, jpg, xls or one of many other files that
can be automated) on a website and stream it to...
|
by: Eddie |
last post by:
Hi all,
I am displaying a number of reports, and giving the users an option to
display them on the web or download them to Excel. If they want the
Excel file, I just use the PHP header command...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |