Is the context for the writing of the blank line in the code you didn't post?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
I don't see the issue.
Debug\cpp.exe ./apptadd date type foo bar
argc == 6
wrote datafile: appointment.txt
#define APPTFILE "appointment.txt"
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
ofstream getfile;
//function for apptadd
//add appointment to the txt file
void addName(string name);
void addappt(string appointment)
{
getfile.open(APPTFILE,ios_base::app);
if ( getfile.is_open() )
{
cout << "wrote datafile: " << APPTFILE << endl;
getfile<<appointment<<endl;
getfile.close();
}
else
{
cout << "ERROR: Could not open the datafile: " << APPTFILE << endl;
exit(1);
}
}
int main(int argc, char* argv[])
{
if ( argc == 1 )
{
cout<<"please enter the date and type of your appointment in a"<<endl;
cout<<"format of <./apptadd date type>"<<endl;
}
else if ( (argc == 2)|(argc ==3)|(argc == 4)|(argc==5) )
{
cout<<"please enter both the date and type of your appointment"<<endl;
cout<<"the format is './apptadd date type' "<<endl;
cout<<"you can use './apptadd help' to find out how to use the";
cout<<" program."<<endl;
}
else if ( argc == 6 )
{
cout << "argc == 6\n";
addappt(string(argv[1])+ " " +string(argv[2])+" " +string(argv[3])+ " " +string(argv[4]) + " "+string(argv[5]));
}
}
BTW, | doesn't mean the same thing as || .
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
still not working.
there still a blank 1st line in the text file.
btw thx for helping me.
Well, if you're always appending, it's not going to go away. Why not delete the file and start fresh?and what is the difference betwwent "|" and "||"?Bitwise versus logical OR.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
what is the difference betwwent "|" and "||"?
|| is "logical OR". | is "bitwise OR".
From this site: http://www.go4expert.com/forums/showthread.php?t=2891
OR
A bitwise OR takes two bit patterns of equal length, and produces another one of the same length by matching up corresponding bits and performing the logical OR operation on each pair of corresponding bits. In each pair, the result is 1 if either the first bit is 1 OR the second bit is 1. Otherwise, the result is zero.
For example:
0101 OR 0011 = 0111
In C and C++, the bitwise OR operator is "|"
For example:
x = y | z;
assigns x the result of "y OR z".
This is different from the C and C++ logical "or" operator, "||" (two vertical bars), which treats its operands as Boolean values, and results in "true" or "false".
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
here is my header file
and i think you have to create a txt file called"appointment.txt"
for compiling it.
#define APPTFILE "appointment.txt"
This output file created by your program?
Where are you looking for this curious blank line?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
the txt file is not creating by my program. it is just a blank new file that allows the user to write things in it.
when finish compiling.
run it in the terminal in linux with
then the text file will be:
So....
#define APPTFILE "appointment.txt"
// ...
ofstream getfile;
// ...
void addappt(string appointment)
{
getfile.open(APPTFILE,ios_base::app);
This isn't creating an output file or appending to it if it already exists? And if it already exists with a blank link at the top, merely appending to it doesn't leave the blank line there?yea, i know. but this is not the main program, there is another .cpp file which reads from that txt file. and i need to display all the contents in that txt file, hence i can't delete the file and start fresh.
If there is another file, why are you not posting all of the code?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
#include <iostream>
#include <fstream>
using namespace std;
#define APPTFILE "appointment.txt"
void addappt(string appointment)
{
ofstream getfile(APPTFILE,ios_base::app);
if ( getfile )
{
cout << "wrote datafile: " << APPTFILE << endl;
getfile << appointment << endl;
}
else
{
cerr << "ERROR: Could not open the datafile: " << APPTFILE << endl;
}
}
int main(int argc, char* argv[])
{
if ( argc < 3 )
{
cout << "please enter the date and type of your appointment in a\n"
"format of <./apptadd date type>" << endl;
}
else
{
string cmdline;
for ( int i = 1; i < argc; ++i )
{
cmdline += argv[i];
cmdline += " ";
}
addappt(cmdline);
}
}
I prepare to run the code. First I get rid of the output file.F:\Projects\scratch>del appointment.txt
I run the code.Debug\cpp.exe ./apptadd date type foo bar
wrote datafile: appointment.txt
At this point the content of "appointment.txt" is as follows../apptadd date type foo bar
I run the code again.Debug\cpp.exe ./apptadd date type foo bar
wrote datafile: appointment.txt
At this point the content of "appointment.txt" is as follows../apptadd date type foo bar
./apptadd date type foo bar
Lather, rinse, repeat?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
the two .cpp files are independent to each other.
the one i am writing is to write things into a txt file.
and the other one is to read things from the txt file.
both programs do not create a txt file.
the txt file is just created by me/the user for storing the information.
so even if i post the other .cpp file, it would not help you to figure out what's happening.
This is the latest code you have posted:
#define APPTFILE "appointment.txt"
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
ofstream getfile;
//function for apptadd
//add appointment to the txt file
void addName(string name);
void addappt(string appointment)
{
getfile.open(APPTFILE,ios_base::app);
if ( getfile.is_open() )
{
getfile<<appointment<<endl;
getfile.close();
}
else
{
cout << "ERROR: Could not open the datafile: " << APPTFILE << endl;
exit(1);
}
}
int main(int argc, char* argv[])
{
if ( argc == 1 )
{
cout<<"please enter the date and type of your appointment in a"<<endl;
cout<<"format of <./apptadd date type>"<<endl;
}
else if ( (argc == 2)|(argc ==3)|(argc == 4)|(argc==5) )
{
cout<<"please enter both the date and type of your appointment"<<endl;
cout<<"the format is './apptadd date type' "<<endl;
cout<<"you can use './apptadd help' to find out how to use the";
cout<<" program."<<endl;
}
else if ( argc == 6 )
{
addappt(string(argv[1])+ " " +string(argv[2])+" " +string(argv[3])+ " " +string(argv[4]) + " "+string(argv[5]));
}
}
This code does not produce the output you see. Unless you're using some crippled compiler like Miracle C or something.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
so even if i post the other .cpp file, it would not help you to figure out what's happening.
Since I don't see the problem with the one set of code, I'm wondering if it's in the other. Do you see the blank line when you open "appointment.txt" in an editor? Or when you're working with the other hunk of code that you're not showing (that is apparently not relevant)?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314