Thread: What is wrong here?????

  1. #1
    Unregistered
    Guest

    Unhappy What is wrong here?????

    I have run through this code many times, and i cant figure out what's wrong with it! why doesn't it work?? i have isolated it to just the following lines:



    Code:
    fstream DataFile("data.txt", ios::in | ios::binary);
    
    if(!DataFile)
    {
      cout << "No file found!!!\n";
      return 0;
    }
    else
    {
      char* data;
      data = new char;
      char tmp;
      while(DataFile.get(tmp)) strcat(data, &tmp);
      cout << data << endl;
      delete data;
      return 0;
    }
    my thoughts are maybe something to do with the strcat? also how do i know i am not overrunning something??

  2. #2
    I think it might have something to do with
    Code:
    char* data;
    data = new char;
    You should do this:
    Code:
    char* data = new char;
    (I think)

  3. #3
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113

    ...

    but that shouldn't matter? anyways i guess what would be a better idea than just asking 'whats wrong??' would be to kind of explain the code.

    main objective:
    to replace 5 lines of variable length within a file.

    the file is somewhat similar to an ini file, containing any number of definitions in the following format:


    <name>
    <line1>
    ...
    <line5>

    what i need to do is find the name of the current definition as loaded into the memory, then replace the 5 lines that follow it with the program-modified version, and close the file. alternately, if the name is not found, i want to append the name along with the five lines at the end of the file.

    now, to my understanding, a file in it's primitive state is simply a string. so, i cant just replace say 10 characters with 15, or i will be overwriting the next line of the file. so i decided to try loading the whole file into memory as a string, and then see what i can do from there. does this seem like a good approach?

    --------
    added
    -------

    maybe an array of strings would be better, each string for one line of the file? then when i want to save it back just slap it all back in there in a row?
    Last edited by Cobras2; 05-22-2002 at 01:19 PM.
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  4. #4
    Unregistered
    Guest
    char* data;
    data = new char;
    char tmp;
    while(DataFile.get(tmp)) strcat(data, &tmp);


    You only alocates memory for one char but you try to squeeze in a whole file?

  5. #5
    Unregistered
    Guest
    Yes, reading the entire file into memory, making changes as needed in file, and writing back to file is part of basic file handling.

    data should be a string, not a single char

    since you don't know how many sets of instructions there will be, a list or a vector may be a better container to hold the file contents. While more straighforward with STL strings and lists, this task is also doable with user defined lists and C style strings.

    string buffer;
    vector<string> fileContents;
    ifstream fin("filename.txt");
    while (!fin.eof())
    {
    //use geline for string class to read first line into buffer
    //place buffer into vector
    if(buffer == targetString)
    //change next five lines of input before sending to fileContents
    else
    //send next five lines of input to fileContents unchanged
    }

    now write all of fileContents back to filename.

  6. #6
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Right after i posted, i began wondering about that whole 'new char' thingie.. realised that would mean i only gots memory for one char. course, making an array would be tough cause i don't know a) how big the file is or even b) how many extra characters i want to put in after modifying it, so anyway I tried using a string and it works fine. I think I am really beggining to almost nearly kinda sorta understand pointers.
    anyways thanks for the help, it works pretty good now. talk to you later!
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM