Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 07-12-2008, 01:43 PM   #1 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Post Several C++ File Handling Queries**Urgent**


hi guys,
Am trying to make a program in C++(turboC++) where a user fills a form and all his details are saved in a file. This file is later used for Login and other purpose. Its something similar to banking.
Queries:
1) How to make the file name as variable?
Quote:
The user gives a USERNAME in the form and file is made with that username. The input of username is taken in a char type array.
2) How to find a specific string in a file and tally it with a char type array?
Quote:
A mother file is created with username & login code. When a user enters his username & code, they are checked via the mother file and if correct, he is taken to user menu. Input is taken in a char type array.
Thats all for now but i'll have more. Please reply ASAP. Thanks a lot
PS: how different is relo from TurboC++? Can i use it as a substitute? Any suggestions?
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 07-12-2008, 03:04 PM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

1. Just use the same char[] you recd as input in the file open argument.
2. Read the file line by line and use string.compare/strcmp functions to check against user input.

Here's a demo program in case you'd like to learn by reading code:
PHP Code:
#include <iostream>
#include <fstream>

using namespace std;

char filename[] = "login.txt";

int checkCred() {
    
char *user, *pass;
    
string line;
    
ifstream ip(filename);
    
cout << "Enter Username: ";
    
cin >> (user = new char);
    
cout << "Enter Password: ";
    
cin >> (pass = new char);
    
string usern(user), passw(pass);
    
string full usern " " passw;
    
// Check against Mother File
    
while ( ip.good() ) {
        
getline(ipline);
        if ( 
full.compare(line) ) {
            
cout << "Correct." << endl;
            return 
0;
        }
    }
    
ip.close();
    
cout << "Incorrect." << endl;
    return -
1;
}

int newCred() {
    
char *user, *pass;
    
string line;
    
cout << "Enter Username: ";
    
cin >> (user = new char);
    
cout << "Enter Password: ";
    
cin >> (pass = new char);
    
string usern(user), passw(pass);
    
string full usern " " passw "\n";
    
// W to User File
    
ofstream us(userios::out);
    
us << full;
    
us.close();
    
// W to Mother File
    
ofstream mo(filenameios::out|ios::app);
    
mo << full;
    
mo.close();
    
cout << "User created." << endl;
    return 
0;
}

int main(int argcchar **argv) {
    
int choice(0);
    while ( 
) {
        
cout << "1. Login" << endl << "2. New User" << endl << "3. Exit" << endl;
        
cout << "Enter choice: ";
        
cin >> choice;
        switch (
choice) {
            case 
1:
                
checkCred();
                break;
            case 
2:
                
newCred();
                break;
            case 
3:
                return 
0;
            default:
                
cout << "Enter the correct option..." << endl;
        }
        
cout << endl;
    }

3. Relo is just an editor, it can use the new Borland compilers. Read Zeeshan's thread in the same section for details on how-to.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 07-12-2008, 03:12 PM   #3 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Several C++ File Handling Queries**Urgent**

hey QwertyManiac...thx a lot pal.
am using this to get the file name as a variable, chk if its correct:
Quote:
user_file.open(c:\\program\\users\\username.c_str( ))
here username is the char array...
also, can u tell me more about using the string.compare/strcmp function while reading a file...the code u gave is not wht am looking for..i need a single function just to find the specific string frm a file so that i can use it again n again...
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 07-12-2008, 03:31 PM   #4 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

You will have to read the file line by line and also write to it the same way. Or instead of newline you can use a comma, or any other delimiter. For every item, check it against the username and password combo.

For example, once you get the new user ID/Pass, write it into the file as:
PHP Code:
$User<space>$Pass<delimiter
Then while reading (Login), read up to a delimiter, and check with the ID/Pass got. In case its a string variable, use the str1.compare(str2) method. In case its a char[] variable, use strcmp(str1,str2) to check if they match.

Make this into a function for reuse.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 07-12-2008, 03:35 PM   #5 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Several C++ File Handling Queries**Urgent**

bingo...thx...i'll try it out n let u knw if it wrks...(the project is related to ma boards practice n so am limited to only a handful of header n lib file )
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 07-12-2008, 03:39 PM   #6 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

Those should suffice! <string.h> (Or cstring) is the one that provides strcmp.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 08-12-2008, 10:21 AM   #7 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Several C++ File Handling Queries**Urgent**

hey guys, chk this code below...i've taken the input of username n password and combined them as a string, so the shud b like this:

abcd+pass bedf+pass ... ... ...


Quote:
void login()
{
clrscr();
gotoxy(10,1);
cout<<"Please provide the Login details:";
char *user_l, *pass_l;
cout << "Enter Username: ";
cin >> (user_l = new char);
cout << "Enter Password: ";
cin >> (pass_l = new char);
string user_lc(user), pass_lc(pass);
string full_lc=user_lc+"+"+pass_lc;
cout<<"Checking our Database for your Login...";
delay(350);
}
now chk this code...here i wht i want is to read frm a file and store it in a STRING untill a space is encountered....as soon as the space is encountered, the STRING should b chkd against the user+pass combination and if incorrect, it should continue...can ne1 help me with this loop??

Quote:
void login_check()
{
clrscr();
fstream check_user(c:\\system\\motherfile.pp, ios::in);
string buffer;
getline(check_user,buffer);
full_lc.compare(buffer);
//incomplete
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 09-12-2008, 12:24 AM   #8 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

Simply use the >> operator over the input file stream object. It reads up to a space or a newline anyway.

PHP Code:
#include <iostream>
#include <fstream>

using namespace std;

int main(int argcchar **argv) {
    
ifstream ip("file.txt");
    
// file.txt contains:
    // "abc+def ghi+jkl"
    
string test("ghi+jkl");
    
char *a;
    while (
ip.good()) {
        
ip >> (= new char); // Reads till space, so: abc+def, ghi+jkl are the two reads.
        
if (test.compare(a)) {
            
cout << "Passed." << endl;
            return 
0;
        }
    }
    
cout << "Failed." << endl;

__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 09-12-2008, 10:57 AM   #9 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Several C++ File Handling Queries**Urgent**

hey guys,
what does this good() functions do??

Quote:
while (ip.good())
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 09-12-2008, 01:47 PM   #10 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

It just checks if the file is still good to read, its a combination of various checks (EOF, bad file, etc). You can simply use an EOF check or let it be implicit like this:

PHP Code:
while (ip
Using .good() is just a good practice, nothing else.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 25-12-2008, 06:49 PM   #11 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Several C++ File Handling Queries**Urgent**

am trying to use "string" data type in Turbo C++ 3.0 but the data type is missing...
i've tried using <string> with "std::" instead of <string.h> but no luck...
guys plz help...

also, ne idea on how to add 2 char array...??
am trying to use this code in turbo c++...

Quote:
string full_m=user_m + "+" + pass_m + "\n";
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 25-12-2008, 09:17 PM   #12 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

The string class is not present in Turbo C++ v3.0. (Try defining one of your own, that you could reuse.)

Use strcat to concatenate several strings.

From CPP.com:
Code:
/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}
This function is available in Turbo C++ v3.0 too, under the string.h include.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 01-01-2009, 07:43 PM   #13 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Several C++ File Handling Queries**Urgent**

uh...1 more query

i have a txt file in this format:

Quote:
Abhi
Mike
Tim
Now i want to find a string in this file and replace it...

like a user wants "Mike" to be replaced with "Jack"...how do i do that?...i can search the string mike but thn how do i replace it???
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 02-01-2009, 01:01 AM   #14 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

That is quite simple. Just like how text editors would write the entire file back on modify+save, you need to do the same.
  1. Open file as ios::in.
  2. Read lines into array.
  3. Close file.
  4. Open same file as ios::out.
  5. Iterate over the array and look for your value, and replace it (replace).
  6. Write each iterated line to file.
  7. Close file.

A sample code, as always:
Code:
#include <iostream>
#include <fstream>
#include <string>

#define BUFF 2048

using namespace std;

int main (int argc, char **argv) {

    ifstream a("sample.txt");
    string line;
    string b[BUFF];
    int i(0);
    while (getline(a, line).good()) {
        b[i++]=line;
    }
    a.close();
    ofstream c("sample.txt");
    for(int j=0; j<i; j++) {
        if (b[j].find("Mike")!=string::npos) // Or any user str
            b[j] = "Jack";
        c<<b[j]<<endl;
    }
    return 0;
}
You can also try string.replace() if its only a part of the string you wish to replace.

Which in our case would look like:
Code:
if (b[j].find("Mike")!=string::npos) // Or any user str
    b[j].replace(b[j].find("Mike"), string("Jack").length(), "Jack");
This will replace Mike with Jack in the string (say "Mike Eats" -> "Jack Eats") leaving the remaining parts intact.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 02-01-2009, 01:57 AM   #15 (permalink)
Wise Old Owl
 
harryneopotter's Avatar
 
Join Date: Feb 2007
Posts: 1,009
Default Re: Several C++ File Handling Queries**Urgent**

hey dude ... for id and password thing u can try this :

make a structure containing the username and password variables.
like
Struct id{ char user [25], pass[25];
} accounts;



than to check the username use it :

fstream fib;
int h=0;
fib.open("Users.txt",ios::in|ios::ate);
while(!fib.eof())
{
off=((h)*sizeof(id));
fib.seekg(off,ios::beg) ;
fib.read((char*)&accounts,sizeof(id));
if(!strcmp(accounts.user,user)) //user being ur variable for user

// ur code
h++;
} //end of while loop



Hope it helps.
__________________
Wats the Favourite Mathematical Function of DIGIT FORUM ????

Its "A SINE WAVE" constant

Y ??

They Both keep going "UP AND DOWN, UP AND DOWN............" !!!!!!!!
harryneopotter is offline  
Old 02-01-2009, 04:29 PM   #16 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Several C++ File Handling Queries**Urgent**

@QwertyManiac...well the code is fine but its Turbo C++ and CBSE dsnt have some of the commands u used...thats y am stuck

wht i was trying is this:

Quote:
char a[10],b[10], x[10];
int pow, t;
ifstream fout("test.dat");
cin>>a>>b;
ofstream ff("test.dat", ios::app);
while(fout.good())
{
fout>>x;
t=strcmp(a,x);
pow=fout.tellg();
if(t==0)
{
fout.seekg(pow);
ff<<b;
}
}
what am doing is that inputing 2 strings a & b...comparing a with data frm file (x) and storing its location in pow...thn if equal, going to that pow position and putting the string b....

the prblm is that b dsnt replaces but comes nxt to that line...
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 02-01-2009, 04:40 PM   #17 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

I've given the simple algorithm you would need to write back to the files, its above the code sample. You will need to write all the lines of the file again, along with the replaced string.

Of course your code would write it as a normal line, and not overwrite, cause its appending at the current file pointer location. There is no overwriting function for file operations.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 02-01-2009, 04:49 PM   #18 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Several C++ File Handling Queries**Urgent**

yeah...i think i gota find the numbr of ch of the string n move the pointer pow-ch back to overwrite...
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 02-01-2009, 05:50 PM   #19 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

Ack. WRONG.

Code:
OPEN FILE AS READ MODE.

LOOP TILL EOF
    GET A VALUE FROM THE FILE (OR A LINE, AS YOU PLEASE).
    >>  IF CURRENT CONTENT MATCHES THE COMPARE, REPLACE IT. <<
    STORE CONTENT OF THIS READ OPERATION IN AN ARRAY.
END LOOP

OPEN SAME FILE AS WRITE MODE (ERASES ALL CONTENT)

LOOP TILL END OF ARRAY
    STORE EACH VALUE OF ARRAY BACK TO THIS NEW FILE.
END LOOP
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 02-01-2009, 08:03 PM   #20 (permalink)
TechFreakiez.com
 
Abhishek Dwivedi's Avatar
 
Join Date: Sep 2006
Location: New Delhi
Posts: 621
Default Re: Several C++ File Handling Queries**Urgent**

ur 200% correct harsh but its a school project for boards so u basically have to scramble up every single command that u find in the book onto this code...lolz
thats y am sticking with seekp n tellg algo
__________________
Personal Log | Star date 05.04.2009: TDF Meet Kanpur was Awesome :D
www.TechFreakiez.com
Abhishek Dwivedi is offline  
Old 02-01-2009, 08:46 PM   #21 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Several C++ File Handling Queries**Urgent**

But that won't do you any good, you can't overwrite. I don't see why you cant implement such a trivial thing, you just need to add an array to your existing code. No functions, nothing.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want GTA SA handling.cfg file. Gigacore Gamerz 2 01-07-2008 10:25 PM
[PayPal] Some URGENT Queries for an Unverified Account Vishal Gupta QnA (read only) 7 23-10-2007 07:28 PM
File handling using GCC/G++ anantkhaitan Programming 16 22-05-2007 04:19 PM
Handling /dev/dsp in C desertwind Open Source 1 04-01-2007 07:32 PM
.:: Urgent: phpBB Queries ::. tuXian QnA (read only) 2 17-09-2005 10:30 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:02 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2