[SOLVED]Unwanted linebreak when outputting to a textfile
Posted: Fri Feb 26, 2010 8:53 pm
Ok, so i wrote a program that asks you for a file name you want to create or edit, and then streams text to it. Its basicly just a simple text editor. After each line of text, there is a line break. My problem is that when i have a string with spaces, it makes each space a new line. Why is it doing this?
if your wondering what main.h is, its just a header i made so i dont have to keep entering different includes + it has some functions i made
Code: Select all
#include "main.h"
int main(){
char fname[20];
string in;
cout<<"Filename: ";
cin>>fname;
ofstream file;
file.open(fname,ios::app);
cls();
cout<<"Enter Text and Press Enter for New Line"<<endl;
while(file.is_open()){
cin>>in;
//if you input "echo hello"
//the created file reads:
//echo
//hello
//instead of:
//echo hello
if(in=="%exit%"){
file.close();
break;
}
//this is where the string is put into the file
//then after, a line break
file<<in<<endl;
}
}
Code: Select all
#include <conio.h>
#include <windows.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <time.h>
using namespace std;
void pause(){
system("pause");
}
void cls(){
system("cls");
}
void exit(){
system("exit");
}
void text_color(char color){
switch(color){
case 'a':
system("color a");
break;
case 'b':
system("color b");
break;
case 'c':
system("color c");
break;
case 'd':
system("color d");
break;
case 'e':
system("color e");
break;
case 'f':
system("color f");
break;
}
cls();
}
void set_title(string title){
string t;
t="title "+title;
system(t.c_str());
return;
}
void space(){
cout<<endl;
}