Thread: Search for .txt file

  1. #16
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I want it to read the first line of text from the file and print it then move on the second line of text and print that etc.I was wondering if anyone knows whats the matter with this code as all it does is print:
    "Name:
    Value:
    Weight:
    Effect 1:
    Effect 2:
    Effect 3:
    Effect 4:
    "
    But doesn't print anything where i want it to print the values from the file but this code doesn't work(lol)!

  2. #17
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    Well, as I see it, you have two options.
    You can simply fix the code that way:

    Code:
    file2.getLine(info,100);
    //where 100 is how much bytes are to be read and info is where to read 
    cout<<"Name:" << info << endl;
    and so on.
    another way is making it a binary file (you'll need to save it binaricly first) like that:

    Code:
    #include <iostream>
    #include <cstring>
    #include <fstream>
    using namespace std;
    struct info{
    //all of the members of the struct 
    char name[100];
    int value;
    };
    int main(){
    ofstream out_file("binary.bin",ios::binary|ios::trunc);
    info save;
    strcpy(save.name,"NAME");
    save.value=10;
    out_file.write((const char *)&save,sizeof(info));
    }
    then, after you have saved the file, load it like that:
    Code:
    #include <iostream>
    #include <cstring>
    #include <fstream>
    using namespace std;
    struct info{
    //all of the members of the struct 
    char name[100];
    int value;
    };
    int main(){
    ifstream in_file("binary.bin",ios::binary);
    info load;
    //it'll be equal to 10;
    load.value=18;
    in_file.read((char *)&load,sizeof(info));
    cout<<"Name:"<<load.name<< endl;
    cout<<"Value:"<<load.value<< endl;
    
    }
    I guess that with some small changes you will also be able to output & input c++ strings(changing variables declarations from char to string,etc).
    this code was compiled using borland c++ compiler and I dont know how will it work on other compilers. give it a shot and tell me what happend.

  3. #18
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I tried the first example and in the second option part 1 doesn't ios::trunc delete everything in the file?So surely if i do that everything in the file will be deleted? andl lastly i tried them all on Dev-C++ and couldn't get them to work i think its due to the "info 'save'" part as it is not declared in my compiler so the programmer must do it(?i think?).Thanx for all of your help though greatly appreciated!
    Last edited by L_U_K_E; 06-14-2006 at 12:27 PM.

  4. #19
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    First of all, you should change the code below...

    Code:
                                          getline(file2,info);
                                          cout<<"Name:" + info << endl;
                                          getline(file2,info2);
                                          cout<<"Value:" + info2 << endl;
    to this...

    Code:
                                          getline(file2,info);
                                          cout<< "Name: " << info << endl;
                                          getline(file2,info2);
                                          cout<< "Value: " << info2 << endl;
    Notice the << operator after the "Name: " and "Value: "

    And post what the .txt file looks like...

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    1
    I agree

  6. #21
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    Quote Originally Posted by L_U_K_E
    ios::trunc delete everything in the file?
    It is. I included it just to make sure the file was empty. the programmer should declare "info save"(info=type & save=name).
    lastly, if you havent compiled and run the first code, the second wont work. BTW did anything worked on your compiler(including the getline)?

  7. #22
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Thanx for the help i changed that but it still doesn't print anything!Also a note to gftmc it does nothing whatsoever i ran it and edited the .bin file entered some values and it prints still nothing however it DOES compile which is some good news as thats half the struggle(lol)!

  8. #23
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    post an update of your code... so we can get another idea...

  9. #24
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    OK here we go:
    Code:
                                          for (i = 0; i < 25; i++)
                                          putchar ('\n');
                                          cout<<"Please entre the name of the ingrediant:";
                                          getline(cin,filename);
                                          std::ifstream file2(("Ingrediants\\" + filename + ".txt").c_str());
                                          getline(file2,info);
                                          cout<<"Name:" << info << endl;
                                          getline(file2,info2);
                                          cout<<"Value:" << info2 << endl;
                                          getline(file2,info3);
                                          cout<<"Weight:" << info3 << endl;
                                          getline(file2,info4);
                                          cout<<"Effect 1:" << info4 << endl;
                                          getline(file2,info5);
                                          cout<<"Effect 2:" << info5 << endl;
                                          getline(file2,info6);
                                          cout<<"Effect 3:" << info6 << endl;
                                          getline(file2,info7);
                                          cout<<"Effect 4:" << info7 << endl;
                                          file2.close();
                                          cin.get();
                                          break;

  10. #25
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    Well obviously... the problem lies within communication with the input file and the program itself...other than that, everything looks ok.. and should work... soo im thinkin that the file that is being opened by the ifstream is to blame?

    this is the only thing i reccomend...instead of doing this...
    Code:
    cout<<"Please entre the name of the ingrediant:";
    getline(cin,filename);
    std::ifstream file2(("Ingrediants\\" + filename + ".txt").c_str());
    make the string into a variable first... and then put the result in the parenthesis..saves confusion? i dunno...but whenever a program miss behaves.. you never know what it could be...
    Code:
    string directoryandfile;
    ...
    ...
    ...
    cout<<"Please enter the name of the ingrediant:";
    getline(cin,filename);
    directoryandfile = "Ingrediants\\" + filename + ".txt";     // you could also try Ingrediants/
    std::ifstream file2(directoryandfile.c_str());
    this preserves the filename for any other use...
    i think this has been reccomended already but... o well ... might as well try...and again post your .txt file or and example file...


    and i don't reccomend what is happening below
    Code:
    for(i=0; i<25; i++)
    putchar('/n');
    a better style would be...
    Code:
    for(i=0; i<25; i++)
    {
         putchar('/n');
    }
    this could help in programmer error type mistakes...
    Last edited by gL_nEwB; 06-16-2006 at 05:16 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM