Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 keeping format of text file?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Justin_




PostPosted: Thu Aug 17, 2006 1:24 am   Post subject: keeping format of text file?

How does one edit a text file programmatically (replace some things in it) while keeping all the formatters in place. i.e. the file looks the exact same as before.

Man, I am outputting to the file with copy but that is bad, bad bad. It is a source text file and it needs to be in a shape to compile.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Aug 17, 2006 11:20 am   Post subject: (No subject)

Don't blame the method of output. Show us how you are editing it.

Providing a tiny amount of information about the problem is not the best way to get useful suggestions.
Justin_




PostPosted: Thu Aug 17, 2006 1:53 pm   Post subject: (No subject)

c++:

//////This code was generated by Justin Moser's brain/////////
/*/@@@@@ issues: can't format the new source code of the stub.
*/

#include <fstream>
#include <iterator>
#include <vector>
#include <windows.h>
#include <sstream>
#include "file.h"

std::string source_edit(std::string input);
//source_edit edits the stub source.  This was the only way I knew how to make a dynamic
//stub
/////////////////////////////////////////////////////////////////////////////////
void generate_stub();
//generate_stub uses the source_edit function to make a fitting stub, then it compiles it
//////////////////////////////////////////////////////////////////////////////

std::vector<File> fileVector;   //this is not good!  I had to do this to supply a unary function
//to the transform algorithm in generate_stub()  I intend to fix this once I conceive of a way.
int main(int argc, char** argv)
{
   if (argc < 3)
   {
      std::cerr << "Specify all the fucking filenames.\n" <<
                   "Syntax: me.exe first.exe second.exe"  <<
                   " new.exe" << std::endl;
      return 0;
   }
   //std::vector<File> fileVector(argc);
   for (int i = 1; i < argc; i++) // '<' because the last parameter is the new file.
   {
        File file(argv[i]);   //construct file with filename param.
        fileVector.push_back(file);
   }
   ////get total size of new file (without stub) ////
   std::vector<File>::iterator index = fileVector.begin();
   unsigned long int TOTAL_SIZE = index->getSize()+(index++)->getSize(); //potential bug here.
   ///////////////////////////////////////////////////////
   generate_stub();       //not working properly yet, needs to format. 
   return 0;
}

void generate_stub()
{
    const char* out_file_name = "generated.cpp";
    const char* mingw_params  = "generated.cpp -o generated.exe";
    std::vector<std::string> transformed_words;
    std::ifstream file("generator.cpp");
    std::ofstream out_file(out_file_name);
    if (file)
    {
       transform (std::istream_iterator<std::string> (file), std::istream_iterator<std::string>(),
                  back_inserter(transformed_words), source_edit);
       copy (transformed_words.begin(), transformed_words.end(),
             std::ostream_iterator<std::string> (out_file));
    }
    //ShellExecute(0, "open", "mingw.exe" ,NULL, mingw_params, SW_SHOW);
    return;
}

std::string source_edit(std::string input)
{
   std::stringstream ss;
   std::vector<File>::iterator i = fileVector.begin();
   if (!input.compare("@file_name"))
   {
      input = "NEWFILEMAN.exe";
   }
   else if (!input.compare("@file1_size"))
   {
      ss << i->getSize();
      input = ss.str();
   }
   else if (!input.compare("@file2_size"))
   {
      i++;
      ss << i->getSize();
      input = ss.str();
   }
   else if (!input.compare("#include<iostream>"))
   {
      input = "#include <iostream>";
   }
   else if (!input.compare("#include<windows.h>"))
   {
      input = "#include <windows.h>";
   }
     return input + " ";
}



Again I appologize for however bad my code is, it is the best I know how to make it. Also, does someone know why using fileVector.end() crashes the program at run time? (It clearly is going past the scope of the vector I think)
Justin_




PostPosted: Thu Aug 17, 2006 1:59 pm   Post subject: (No subject)

c++:

copy (transformed_words.begin(), transformed_words.end(),
             std::ostream_iterator<std::string> (out_file));


This would be the relevant part of the code. I tried (out_file, "\n") but that is not compile worthy either. Any suggestions?
wtd




PostPosted: Thu Aug 17, 2006 5:30 pm   Post subject: (No subject)

Well, it doesn't appear that you're including the algorithm header, or using namespace std.
Justin_




PostPosted: Thu Aug 17, 2006 7:49 pm   Post subject: (No subject)

Why does it compile then if I need to those?
Justin_




PostPosted: Thu Aug 17, 2006 8:05 pm   Post subject: (No subject)

I didn't think it would be that complicated. I understand that it would be easier to use a script, but still, it shouldn't be that complicated.

Does anyone recall opening a file and reading the contents?
Do they remember storing the contents in a variable?
Do they remember editing it a little and then putting the contents back into the file?

Perhaps php and perl have scewed my perception a little. With them reading a file into a variable is a piece of cake because it magically keeps it formatted as well.
Justin_




PostPosted: Thu Aug 17, 2006 8:31 pm   Post subject: (No subject)

Okay I found a way... Yet again, C-Style arrays to the rescue. wtd, might you know of a way to maintain c++ standards compliability and complete this task?
Sponsor
Sponsor
Sponsor
sponsor
Justin_




PostPosted: Thu Aug 17, 2006 8:32 pm   Post subject: (No subject)

c++:

#include <fstream>
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

int main()
{
   char cstr[255];
   std::ifstream file("generator.cpp");
   std::ofstream out("new.txt");
   while (!file.eof())
   {
      file.getline(cstr,255, '\n');
      out << cstr << "\n";
   }
   return 0;
}
wtd




PostPosted: Thu Aug 17, 2006 9:22 pm   Post subject: (No subject)

A few changes:

code:
#include <fstream>
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::cin;

int main()
{
   std::string line;
   std::ifstream input_file("generator.cpp");
   std::ofstream output_file("new.txt");

   while (!file.eof())
   {
      std::getline(input_file, line);
      output_file << line << endl;
   }

   return 0;
}


I'll see if I can write an example of how to read lines from a file into a vector using copy. It's been awhile, so I'm a bit fuzzy on the hoops I have to jump through. Smile
Justin_




PostPosted: Thu Aug 17, 2006 10:58 pm   Post subject: (No subject)

file.eof() should be: input_file.eof()

Yes that way is much better. It even saved me! I was converting the char array to a string in my replacer function and after doing some comparisons, it was really wierd actually, it would cause the program to hang or create a memory error.
The reason this was wierd is because after the first compare everything was smooth. If a second comparison matched it caused it to stop a few lines after the comparison and generate no error. If three comparisions matched it caused a memory error and the program would only get a few lines.

Wierd huh? How would you explain that?

The form of comparison I used was: if (str == " a line in the program").

When I changed to std::getline the problem was resolved... I have no other guess but to assume the problem was in the implicit conversion between string and char.
wtd




PostPosted: Thu Aug 17, 2006 11:06 pm   Post subject: (No subject)

When at all possible, using std::string is much nicer than using char arrays.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: