It would appear that you are passing a null pointer to some function. Check for null before passing the value to the function.

And it's difficult for us to observe from the 0 lines of code you posted. ;)

It would appear that you are passing a null pointer to some function. Check for null before passing the value to the function.

And it's difficult for us to observe from the 0 lines of code you posted. ;)

LOL, here is it, but it's confusing i think.
Don`t mind the text between cout, i`m brazilian.

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
using namespace std;
int main() 
{
int a;
string *n;
char *pc;
ofstream *p;
cout << "Quantos arquivos de texto vc quer?";
cin >> a;
p = new ofstream[a];
n = new string[a];

for (int i=0;i!=a-1;i++)
{

cout << "Escreva o titulo do " << i+1  << " arquivo:";
getline(cin,n[i]);
pc = new char[n[i].size()];
strcpy(pc,n[i].c_str());
p[i].open(pc);  // when i take it out i get no error, i dont know why...

}

cout << "Feito" << endl;

return 0;
}

Edited 6 Years Ago by hey joe: n/a

Are you passing an empty string to .open()?

No, because the pointer pc is holding the char array.
First i made that program that just ask for a title for a file and then what will be in it, but it was not taking the "spaces", so i tried to use a string, but i have to convert it to char array before passing it in .open.
The program works when this line pc = new char[n.size()]; is outside the for loop and it's bad because i cant get getline information on the loop, it has to be before that line to give the size of the array.

It also works when i delete this line: p.open(pc); i cant see the problem with it.

Edited 6 Years Ago by hey joe: n/a

No, because the pointer pc is holding the char array.

Did you verify this be, say printing out the text of the file to be opened? Like,

cout << "pc = \"" << pc << "\"\n";

Ya see, when you input a number it leaves the newline in the stream. This will pretty much ensure that the filename is empty.

First i made that program that just ask for a title for a file and then what will be in it, but it was not taking the "spaces", so i tried to use a string, but i have to convert it to char array before passing it in .open.

You don't have to. In fact, you have the other option right there in your strcpy call.

But an empty string, a size() of zero, going to new -- I wouldn't expect great things. Fix your user input problems upstream.

Edited 6 Years Ago by Dave Sinkula: n/a

You don't have to. In fact, you have the other option right there in your strcpy call.

I tried to do it but when i put the string in the first line of getline function i just get this error:
error C2664: 'strcpy' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'

i want to know if it is a problem to use new inside a loop...

Edited 6 Years Ago by hey joe: n/a

What I was saying was something akin to...

int main() 
{
   string input;
   cout << "Quantos arquivos de texto vc quer?";
   int a;
   if ( getline(cin, input) )
   {
      istringstream iss(input);
      if ( iss >> a )
      {
         ofstream *p = new ofstream[a];
         for ( int i = 0; i < a; ++i )
         {
            cout << "Escreva o titulo do " << i+1  << " arquivo:";
            getline(cin,input);
            p[i].open(input.c_str());
         }
         cout << "Feito" << endl;
      }
   }
   return 0;
}

what i was saying was something akin to...

thank you forever

This article has been dead for over six months. Start a new discussion instead.