problem allocation of memory?!?! copy part of a sting....

Ok, so I am trying to read from a text file, I can converted each line into a string and print it one line at a time, that works.

now i want to take part of each string, and copy it to later save to another text file, but when i try to save part of the text file into another string, it gives me a runtime error, i debugg it and it says someting about "out of range memory allocation", i have looked at tons of examples and been working on this for over 8 hours, and have been geting no where... someone please take a look at this for me and see what im doing wrong or how i should fix this....

it would be much appreciated!!!







#include <iostream>
#include <fstream> //used to read and write files
#include <string>


using namespace std;

void main(){

string fileRead, fileWrite, copiedString, newString;


cout << "What file do you wish to read/analsys data?" << endl;
cin >> fileRead;

ifstream inFile; //declares inFile as a stream....
inFile.open(fileRead); //user inputs the name of the file to read from
if(!inFile){ //if file cant open, then output message
cout << "Cannot Open File Specified" << endl;
exit(1);
}



string line;
int x;
char buffer[255];



while(getline(inFile, line)) //This loops every line
{

cout << line << endl;

/*string s3 (line, 8, 3); //this also gives me the same problem

cout << "s3 is" << s3 << endl;
cin >> x;
*/



// it runs now but the buffer output is all messed up.... i had to take out the line.copy(x,y,z) so i dont think it knows to copy from the string 'line'
//std ::string copy(buffer, //something about this section of code is giving a runtime error, "out of range memory allocation"
// 6,
// 4);
//buffer[6] = 0;
//cout << "buffer is" << buffer << endl;
//cin >> x;


}


cout << line << endl; // since this is outside the while, it only gets the last line.....
inFile.close();


//char buffer[256]; //can hold up to 256 characters

//while(inFile){
// inFile.getline(buffer,10);
// cout << buffer <<endl;

// inFile.close();
//




//output part

cout << "What file do you wish to write to?" << endl;
cin >> fileWrite; //file name is string, such as "2_bike.txt"


ofstream outFile; //sets myfile as a variable to open my txt
outFile.open(fileWrite); // opens fileName, which user designates what location/what file to open
outFile << copiedString << endl; //writes "yes" to myfile
outFile.close(); //closes myfile
}





btw, some of the stuff i commented out in the while loop is what i have tried and have had problems with...


if you have any questions on what i mean, just let me know
Last edited on
[code] "Your code goes here" [/code]
it does not compile.
_main must return int
_exit is not declared
_no matching function std::fstream::open( std::string &)
FIXES (that I can find):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <limits>  // For numeric_limits
#include <fstream>
#include <string>
using namespace std;

void main() // INVALID. MAIN must return int.
{
  string fileRead, fileWrite, copiedString, newString;

  cout << "What file do you wish to read/analsys data?" << endl;
  cin >> fileRead;

  ifstream inFile; // This is fine.
  
  inFile.open(fileRead); // Problem. .open() method of ifstream only accepts cstrings.
  // Use the .c_str() method of std::string to get one!
  // The syntax looks like: " fileRead.c_str() " (Minus the quotes)
  
  if(!inFile) // Good.
  { 
    cout << "Cannot Open File Specified" << endl;
    exit(1); // You need <cstdlib> to use exit()
  }

  string line;
  //int x;
  char buffer[255];

  while( getline(inFile, line) ) // You read one line in
  {
    cout << line << endl; // You print it out.
    
    // Now I'm guessing you want to make a substr, starting at 8 for 3 chars.
    // Use .substr() method of std::string.
    string s3 = line.substr(8, 3); // Substring of about 3 chars at pos 8.
    // If pos 8 is beyond the string a exception will be thrown, program will end.
    
    
    ////// string s3 (line, 8, 3); //this also gives me the same problem
    
    // So this here should now work:
    cout << "s3 is" << s3 << endl;
    
    // Get an integer.
    // I think you're trying to put in stops.
    // I suggest cin.ignore()
    // cin >> x;
    cin.ignore( std::numeric_limits< std::streamsize >::max(), '\n');
    

    // I read the note here. I'm suggesting you try it now. 
    int length = line.copy(buffer, 6, 4); // Copy from line, at pos 6, 4 chars.
    buffer[length] = '\0';
    cin.ignore( std::numeric_limits< std::streamsize >::max(), '\n');

  }
  //cout << line << endl; // Don't see a problem. Not nessessary.
  inFile.close();

// ------------------------- OUTPUT

  cout << "What file do you wish to write to?" << endl;
  cin >> fileWrite;

  ofstream outFile; 
  outFile.open(fileWrite); // .c_str()!!!!
  
  // Where did you even create or initalkize copiedString. I never saw it.
  outFile << copiedString << endl;
  outFile.close();
  
  // RETURN 0!!
}
Topic archived. No new replies allowed.