Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
        ofstream f;
char c;
f.close();
f.open(s.c_str(),ios::out);
if(f.is_open()) {
cout<<"Press <q> to save file";
char *text;
int count=0,count1;

while(1)
{   system("stty raw -echo");
    cin>>c;
    cout<<c;
    system("stty cooked echo");
    text[count]=c;
    ++count;

        if(c=='q')
    {
            text[count]='\0';
                while (text[count1]!='\0')
        f.put(text[count1++]);
    f.close();
            break;
                }
        }


please help me out in this syntax i was not able to write data in file
Why this code gives segmentation fault
how i can resolve this problem

[edit]Code Block added - OriginalGriff[/edit]
Posted
Updated 25-Apr-11 22:37pm
v3

No, you won't be able to write.

Do you want a list of errors, or just the problem you are reporting?

Just the one then:
char *text;
        ....
text[count]=c;
Where is "text" pointing when you access element "count"? What value have you given it? How many elements are allocated to it?

And just to be nice, here is hint about another one for when you fix that one:
while(1)
is not a loop you will ever get out of...
 
Share this answer
 
Comments
Albert Holguin 26-Apr-11 8:15am    
he does have that break; statement inside the while(1)...
Sergey Alexandrovich Kryukov 26-Apr-11 15:35pm    
Agree with Albert, but the rest of the answer is adequate.
This should be a get-back-to-basics note.
--SA
Niklas L 26-Apr-11 16:57pm    
There was no break in the original version. The question was updated, and the Griff was right.
Albert Holguin 26-Apr-11 19:08pm    
oh i see...
Niklas L 27-Apr-11 14:08pm    
Don't worry. I was thinking the same thing when I saw it :)
text[count]=c;


text does most likely not point to any writable memory since you have not assigned or allocated any. I suggest you use the debugger to easily spot these problems.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Apr-11 15:37pm    
Or gosh, OP also thinks in terms of 1-based array indexing.
Back to basics!
My 5 for the answer.
--SA
Besides what others have already pointed out, count1 is being used as an index into the array without being initialized... although the array isn't allocated at all so that problem won't come up until you clear that one up first.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900