How to Access this C++ Database?

At first i study accessing C++ database using this data file formation written in notepad let say "record.txt" is the name of database.

E1-1000
John_Wright
1
200

E1-1001
Robert_Hood
2
300

The "record.txt" is located at "C:\record.txt" and my code look like this.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
main()
{
clrscr();
char pinCode[100],Name[100],Buffer[100];
int Position,Rate,Compare=0;

cout<<"Enter Pin: ";
cin>>pinCode;
ifstream fin("C:\record.txt");
while(!fin.eof())
{
fin.getline(Buffer,100);
Compare=strcmp(pinCode,Buffer);
if(Compare==0)
{
fin>>Name;
fin>>Position;
fin>>Rate;
cout<<Name<<endl<<Position<<endl<<Rate<<endl;
break;
}
}
fin.close();
getch();
return 0;
}

This Code & Database work fine after you input Pin code the information of the pin code will shown on the screen,but now the "PROBLEM GIVEN" is the "record.txt" is located in your Desktop "C:\Documents and Settings\Desktop\record.txt" and the Data file formation written in "record.txt" look like this.

John_Wright,E1-1000,1
Robert_Hood,E1-1001,2

Note:You are not able to edit this Database, you also not able to create new database, you also not able to remove your database in your desktop and move to C:\ or create desktop create shortcut comes from C:\.The database strictly located at "C:\Documents and Setting\Desktop\record.txt".

This is the problem and hard to search the pin code located at the center of data information and also hard to connect to desktop location but in C:\ its error free. I hope someone could help and thanks in advance...

This is the problem and hard to search the pin code located at the center of data information and also hard to connect to desktop location but in C:\ its error free. I hope someone could help and thanks in advance...


I don't understand what you want to say. Please, repeat it, and be more concise.
You have to read carefully for you to understand!
As i said How do you access this kind of database Located at C:\Documents and Setting\Desktop\record.txt and the database is look like this...

John_Wright,E1-1000,1
Robert_Hood,E1-1001,2

Now to indicate what this data stand for is like this John_Wright = Name,E1-1000 = pinCode,1 = Position,Robert_Hood = Name,E1-1001 = pinCode,2 = Position.

Now the task is:
1.When you run the Program the system will ask you to enter your pincode.
2.After you enter your pincode, the system will going to search the pincode in your database.
3.Then after searching your pincode in your database, the system will going to print your pincode information on the screen.
4.And if your Position = 1 then the Rate = 200,and if your Position = 2 then the Rate = 300.

Example:
step-1(the system will ask to enter pincode)
Enter Pin Code: E1-1000
step-2(the system will search your pincode in your database located at C:\Documents and Setting\Desktop\record.txt) And the system will print on the screen the pincode information.
Name : John_Wright
Position: 1
Rate : 200

Note:You are not able to edit this Database, you also not able to create new database, you also not able to remove your database in your desktop and move to C:\ or create desktop create shortcut comes from C:\.The database strictly located at "C:\Documents and Setting\Desktop\record.txt".
Check this tutorial
http://www.cplusplus.com/doc/tutorial/files/

That Tutorial works for this kind of database that located on "C:\record.txt and the database are look like this...

E1-1000
John_Wright
1
200

E1-1001
Robert_Hood
2
300

What if the database are located at "C:\Documents and Setting\Desktop\record.txt" and the database are look like this...

John_Wright,E1-1000,1
Robert_Hood,E1-1001,2

Note:You are not able to edit this Database, you also not able to create new database, you also not able to remove your database in your desktop and move to C:\ or create desktop create shortcut comes from C:\.The database strictly located at "C:\Documents and Setting\Desktop\record.txt".
Well, use the formatted extraction from a stream operator >>. You can also use stringstream objects. Look that up in the reference.

By the way: This is OBVIOUS HOMEWORK.
Well, use the formatted extraction from a stream operator >>. You can also use stringstream objects. Look that up in the reference.

By the way: This is OBVIOUS HOMEWORK.

No, this is not a homework, this is what i got in my exam applying in the company that until now i need to resolve the given problem coz' the company given me a chance to retake this kind of exam.The only problem is i cannot connect to the database located at "C:\Documents and Setting\Desktop\record.txt" and the pin code is placed at the center of horizontal type data information that look like this...

John_Wright,E1-1000,1
Robert_Hood,E1-1001,2

where E1-1000 and E1-1001 is the Pin code in the given horizontal type data information.


This is now my code in formatted version...

#include<iostream>
#include<conio>
#include<fstream>
#include<string>

using namespace std;

int main()
{
clrscr();
char pinCode[100],Name[100],Buffer[100];
int Position,Rate,Compare=0;

cout<<"Enter Pin: ";
cin>>pinCode;
ifstream fin("C:\Documents and Setting\Desktop\record.txt");
while(!fin.eof())
{
fin.getline(Buffer,100);
Compare=strcmp(pinCode,Buffer);
if(Compare==0)
{
fin>>Name;
fin>>Position;
fin>>Rate;
cout<<Name<<endl<<Position<<endl<<Rate<<endl;
break;
}
}
fin.close();
getch();
return 0;
}


That is the code that i have now but still cannot connect to data base located at "C:\Documents and Setting\Desktop\record.txt"

ifstream fin("C:\Documents and Setting\Desktop\record.txt");


And also i cannot search the Pin code placed at the center of horizontal type database information.

John_Wright,E1-1000,1
Robert_Hood,E1-1001,2

fin.getline(Buffer,100);
Compare=strcmp(pinCode,Buffer);


If the database information is in vertical type, there is no problem and i can search it that look like this...

E1-1000
John_Wright
1
200

E1-1001
Robert_Hood
2
300


But there is the reminders and this is the reminders.

Note:You are not able to edit this Database, you also not able to create new database, you also not able to remove your database in your desktop and move to C:\ or create desktop create shortcut comes from C:\.The database strictly located at "C:\Documents and Setting\Desktop\record.txt".
If this really is an exam for a company, then you should know this stuff, otherwise you shouldn't be working there probably. >_>
If this really is an exam for a company, then you should know this stuff, otherwise you shouldn't be working there probably. >_>


As i said the company is given me a a chance to retake this kind of exam actually we are 15 applicants and sad to say no one passed the exam xD...But after the exam they conduct an interview 1 by 1 and i passed the interview, the only problem is the given database is something weird or it is a something a puzzle and 1 thing is the location of database is located on the desktop that normally it is located in C:\record.txt.
closed account (z05DSL3A)
The only thing I see weird about the location, is the lack of a user directory
eg. C:\Documents and Setting\#user id#\Desktop\record.txt
but that doesn't mean to say that they haven't set up a 'Desktop' directory under 'C:\Documents and Setting'.

What is the error you are getting?
ifstream fin("C:\Documents and Setting\Desktop\record.txt");
should be

ifstream fin("C:\\Documents and Setting\\Desktop\\record.txt");
or
ifstream fin("C:/Documents and Setting/Desktop/record.txt");


If you want to get to your own desktop, you use:
1
2
3
4
5
6
7
std::string path;
const char* profile_path = getenv("USERPROFILE");
if (profile_path)
    path = profile_path;

path += "\\record.txt";
std::ifstream fin(path.c_str());
Last edited on
Topic archived. No new replies allowed.