|
it's buffered, so you might not see the data in the file until the buffer flushes.
|
|
|
|
|
so the fstream's write operation will not be blocked?
|
|
|
|
|
I currently program in C# and decided to make a move to C++. I am finding it a little more difficult than I thought. I mean I am having trouble even reading a file. I am using XCode on Mac OS X Snow:
#include <cstdio>
#include <fstream>
#include <istream>
#include <ostream>
#include <iostream>
int main () {
using namespace std;
ifstream in("/Users/Dixon/MyFile.rtf");
if (not in)
perror("/Users/Dixon/MyFile.rtf");
else {
int x(0);
while (in >> x)
{
cout << x << '\n';
}
cout << "Ending....";
in.close();
}
}
Output:
[Switching to process 16406]
Running…
Ending....
Debugger stopped.
Program exited with status value:0.
Ok, now I understand I am not getting any errors when opening this file. The problem is it isn't reading anything from it. I have put 5 lines in there (just one word on each line). Now according to my book (Exploring C++ by Apress), this hsould work. This code is pretty much exactly from their book. Except in their book they used a list1302.in file as I used a RTF file. Maybe that is the problem? I really don't know what an IN file is exactly (coming from Windows world)
|
|
|
|
|
Ok im back tracking on my reading.. so the point is to read integers from a file. So I changed the words in my rtf file to just numbers and I still get nothing?
|
|
|
|
|
An rtf file includes formatting - so there is more in the file than just what you typed. Try using a plain text file with several integers typed into it.
|
|
|
|
|
I still can't seem to get it to work. I saved that RTF file as TXT and I also coded this to try to get it to work:
ofstream out("/Users/Dixon/test1.txt");
if (not out)
perror("/Users/Dixon/test1.txt");
else {
out << "1" << "2" << "3" << "4" << "5" << "6";
out.close();
ifstream in("/Users/Dixon/test1.txt");
if (not in)
perror("/Users/Dixon/test1.txt");
else {
int x(0);
while (in >> x)
{
cout << x << '\n';
}
in.close();
}
}
So I made it create a file with the words 123456 in it, close it then open it to read.. but it doesn't read!
|
|
|
|
|
I have also tried this: (from: http://www.cplusplus.com/doc/tutorial/files/[^])
#include <iostream>
#include <fstream>
#include <string>
int main () {
using namespace std;
string line;
ifstream myfile("/Users/Dixon/test1.txt");
if (myfile.is_open())
{
while (! myfile.eof())
{
getline(myfile,line);
cout << line << endl;
}
myfile.close();
}
else {
cout << "Unable to open file";
}
return 0;
}
But I seem to get:
[Switching to process 17059]
Running…
Exploration 13(17059) malloc: *** error for object 0x10000a700: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all
(gdb)
If I change this:
while (! myfile.eof())
to:
while (myfile.eof() != false)
it runs but still does not read anything
|
|
|
|
|
Uhm... I tried something... I used NetBeans 6.8 instead of Xcode and the exact code compiled and ran just fine. It actually read the data. What could be the cause of that?
|
|
|
|
|
I don't know - I'm not familiar with either Xcode or NetBeans. Hopefully, someone else will have an idea for you.
|
|
|
|
|
Just being curious, but what do you use? I used Visual Studio for C#, but I wanted to try to find a program that would work on both windows and mac (thats where I found netbeans).
|
|
|
|
|
I've written code for a few different computers, but so far not the mac.
Lately I've been using:
Visual Studio for C++ on the PC,
CodeWarrior/Freescale for C cross compiled from the PC to a microcontroller, and
Visual C++ 1.52 plus some add-ons for C for an 80386EX embedded system.
None of this code is using iostreams & I have to consider myself weak in that area at the moment. I would have just looked at the responses you got to see what I could pick up, but seeing rtf rather than plain text was a glaring issue for me. In the last week I've had to deal with a couple of mismatched formatting issues.
modified on Thursday, January 21, 2010 12:31 AM
|
|
|
|
|
Jacob Dixon wrote: #include <cstdio>
#include <fstream>
#include <istream>
#include <ostream>
#include <iostream>
Of these, only fstream and iostream are needed.
Jacob Dixon wrote: if (not in)
Have you tried:
if (in.fail())
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I'm wondering if there is something else this book is talking about? It says C++. But the code above is exactly what the book put in. Interesting...
I take it you didn't see my post before this one but it seems it works fine in netbeans, but in xcode it wouldn't work. I got the code that I was having trouble with to work perfect in netbeans. What could cause this?
|
|
|
|
|
hello, i'm usig this spin control, it works fine, in my application i draw some lines when the spin control increases or decreases an edit box,nice, but when the control change the acceleration it seems like iDelta and iPos (from UDN_DELTAPOS) change their behavior, because in that moment the line plotted is wrong , but after and before that moment all works fine, and the variable shown in the buddy edit box is always well. I guess that in acceleration changes happens somethig that i dont know whith spin control. Any ideas ? i can`t find any good information about how this controls works, specially the acceleration thing.
|
|
|
|
|
Hi to all,
I have a small doubt.Is it possible to use arithmetic operators between two char* variables, like bellow
char* conc(const char* c1,const char* c2)
{
char* c3=c1+c2
return c3
}
Above code is giving error "adding two pointer variables is invalid". Please tell me how to concat(add) two char*.
To invent something, you need a mountain of junk in your mind.
---------------------Thomas alva edison
|
|
|
|
|
You could use strcat()[^] to concatenate strings.
Alternatively, you could use one of those string classes (such as CString) which will allow you to just use + (the addition operator) to add two strings.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Rajesh R Subramanian wrote: You could use strcat()[^] to concatenate strings.
Actually my requirement is "not use the strcat() function".
To invent something, you need a mountain of junk in your mind.
---------------------Thomas alva edison
|
|
|
|
|
If you have to stick to the two pointer of char in order to concat them.
You have to creat a new char * with the length of both. Then copy both to the one.
I would suggest using stings.
You could also create a string and use += to do it.
I guess that is your homework, isn't it? Sounds like.
Cheers
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
|
|
|
|
|
sampath-padamatinti wrote: Actually my requirement is "not use the strcat() function".
Why?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Are you new around here? It probably is one of these:
- I let others do my homework
- I am troll
- strcat() isn't safe enough; I prefer strcat_s()
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that. [The QA section does it automatically now, I hope we soon get it on regular forums as well]
|
|
|
|
|
Which one, sir?
I suppose: strcat isn't safe enough, I prefer mess up with pointers myself.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
My interviewer thrown a question like:
He wants to implement a function where he needs to takes two char* parameters as input at the end it should return a char* by concatenating these two char* values(without using strcat(), strcmp() functions).
required function prototype is
char* concat(const char* c1, const char* c2)
I am unable to solve this one. Please give me the approach
Thanx in advance.....
To invent something, you need a mountain of junk in your mind.
---------------------Thomas alva edison
modified on Thursday, January 21, 2010 2:21 AM
|
|
|
|
|
sampath-padamatinti wrote: I am unable to solve this one. Please give me the approach
David did, here.
However, if you're stuck with the const requirement on the first argument, then:
char * concat(const char * c1, const char * c2)
{
size_t size[2] = {strlen(c1), strlen(c2)};
char * c = new char[size[0]+size[1]+1];
strcpy(c, c1);
strcpy(c+size[0],c2);
return c;
}
if you can't use strlen and/or strcpy :
char * concat(const char * c1, const char * c2)
{
size_t len=0;
char * c;
const char *p;
p = c1;
while (*p++) len++;
p = c2;
while (*p++) len++;
c = new char[len+1];
p=c1;
while (*c=*p++) c++;
p=c2;
while (*c++=*p++) ;
return (c-len-1);
}
Test program:
void main()
{
const char * str1 = "hello, ";
const char * str2 = "folks!";
char * result = concat(str1, str2);
printf("%s\n", result);
delete [] result;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Here is the function as per your requirement:
char* concat(const char* c1, const char* c2)
{
int size1 = 0, size2 = 0;
for (int i = 0; c1[i] != '\0'; i++)
size1++;
for (int j = 0; c2[j] != '\0'; j++)
size2++;
char* result = new char[size1+size2];
for(int i=0; i<size1; i++)
{
result[i] = c1[i];
}
for(int j = 0; j <= size2; j++)
{
result[size1+j] = c2[j];
}
result[size1+size2] = 0;
return result;
}
|
|
|
|
|
Note that in what you are doing, you are dealing with 6 variables. You are talking about the 3 char * variables, but as you said, those are pointers. What you must remember is that pointers are pointers - they are used to point to things, in this case arrays of char. These arrays are not automatically provided. You, the programmer, are responsible for providing them for the pointers to point to.
You have to keep in mind the distinction between pointer and "pointee". Once you do that, you would not say that you want to add or concatenate 2 pointers. Instead, you want to concatenate the 2 strings contained in 2 char arrays and put the result in a third char array. You use the pointers to access the char arrays.
Good luck
|
|
|
|