the function is used to write things into a txt file.
the function works quite well except there is a blank first line in the txt file.

i think the error is caused by "ios_base::app", which always write to the end of the file.
i want to write to the end of the file, but i don't want a blank 1st line. can anyone help me to solve this problem?
|

Is the context for the writing of the blank line in the code you didn't post?

This is my whole .cpp file.

#include "main.h"

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]));
    }
}

I don't have your header, so I can't compile it.

Code tags are not difficult:
http://www.daniweb.com/forums/misc-explaincode.html

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"


#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);

commented: Obviously you didn't read what you quoted. -4

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 || .

still not working.
there still a blank 1st line in the text file.
btw thx for helping me.
and what is the difference betwwent "|" and "||"?

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.

commented: Beat me twice in a row in posting. :) +23

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".

Well, if you're always appending, it's not going to go away. Why not delete the file and start fresh?
Bitwise versus logical OR.

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.

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?

This output file created by your program?
Where are you looking for this curious blank line?

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

./apptadd 1 this is a file

then the text file will be:

<blank line>
1 this is a file

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?

#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?

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.

commented: Post ALL your code, you don't have the expertise yet to decide whether or not a piece of code is or is not relevant kid. -4

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.

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)?

commented: I'm willing to wager the other code which she thinks isn't relevant, is apparently relevant! +22
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.