How can i write text in c++ console without breaking the lines - c++

how can i input text in c++ console without breaking the input in one line at a time?
If i use cin i can input one string each time plus i cannot edit the input (except if i edit the string but this wont help)
Is there any way to input strings (with multiple lines) but not to break the string in one line at a time?
I am running ubuntu 12.04

Who is writing? Is it you, or some program??
Your terminology is unusual: generally programmers take the point of view of the computer!
What you write by typing on your keyboard is an input to some program (which reads it).
If you want an editable input (to the program, so "written" or typed by the human user), consider using GNU readline (on Linux), or perhaps ncurses
If you want to format the program's output (which the user would read with his eyes), you'll generally need to code that formatting explicitly. Perhaps ANSI escape codes might be useful (but using them might make readline or ncurses unhappy).
See also this answer and the references I gave there.

Related

Allow a user to not only input multliline text but move up and edit previously entered lines

Im working on a crude console based text editor in C++ kind of like nano. I've already figured out a basic way of inputting multiple lines of text and writing it to a file correctly (input ends when they enter a code: //end). However, at the moment the user is unable to move upwards (using arrow keys) and edit lines that they have entered. For some additional information I'm doing this with a getline() loop, writing files with ofstream, and am storing the users text in a string vector with each element being an entered line. How might I implement the ability to work with a body of text in such a way?
For advanced use, you need access to the console API.
For a simpler version, look at the primitive visual editors.
Editing a line consists of moving to the line, printing out the content, and then letting them insert or delete characters on that line.
Look at sed or ed or even vi.

Fortran write inserts 2 newlines w

In a simple test routine i am curently writing in Fortran90, the string output is longer than the screen. When using the standard write(,) statement, the output in the Instead of simply add a new line and continue on the next line, a second newline is added before continuing the output.
Example code:
write(*,*) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
Intel Visual Fortran Composer XE 2013 SP1 output:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Why that is ? Where does this extra newline come from ?
By using list-directed output, which you do with the second * in the arguments to the write statement, you surrender precise control of output formatting to the whims of the compiler writers. Seize back control with a specific format, something like write(*,'(a128)') (replacing 128 with whatever is appropriate for the length of string you want to write). See what happens then.
It might amuse you to learn to use the repeat function for, well, repetitive strings.

How to keep characters in C++ from combining when outputted to text file

I have a fairly simple program with a vector of characters which is then outputted to a .txt file.
ofstream op ("output.txt");
vector <char> outp;
for(int i=0;i<outp.size();i++){
op<<outp[i]; //the final output of this is incorrect
cout<<outp[i]; //this output is correct
}
op.close();
the text that is output by cout is correct, but when I open the text file that was created, the output is wrong with what look like Chinese characters that shouldn't have been an option for the program to output. For example, when the program should output:
O dsof
And cout prints the right output, the .txt file has this:
O獤景
I have even tried adding the characters into a string before outputting it but it doesn't help. My best guess is that the characters are combining together and getting a different value for unicode or ascii but I don't know enough about character codes to know for sure or how to stop this from happening. Is there a way to correct the output so that it doesn't do this? I am currently using a windows 8.1 computer with code::blocks 12.11 and the GNU GCC compiler in case that helps.
Some text editors try to guess the encoding of a file and occasionally get it wrong. This can particularly happen with very small amounts of text because whatever statistical analysis is being used just doesn't have enough data to make a good conclusion. Window's Notepad has/had an infamous example with the text "Bush hid the facts".
More advanced text editors (for example Notepad++) may either not experience the same problem or may give you options to change what encoding is being assumed. You could use such to verify that the contents of the file are actually correct.
Hex editors/viewers are another way, since they allow you to examine the raw bytes of the file without interpretation. For instance, HxD is a hex editor that I have used in the past.
Alternatively, you can simply output more text. The more there is, generally the less likely something will guess wrong. From some of my experiences, newlines are particularly helpful in convincing the text editor to assume the correct encoding.
there is nothing wrong with your code.
maybe the text editor you use has a default encoding.
use more advanced editors and you will get the right output.

getline() text with UNIX formatting characters

I am writing a C++ program which reads lines of text from a .txt file. Unfortunately the text file is generated by a twenty-something year old UNIX program and it contains a lot of bizarre formatting characters.
The first few lines of the file are plain, English text and these are read with no problems. However, whenever a line contains one or more of these strange characters mixed in with the text, that entire line is read as characters and the data is lost.
The really confusing part is that if I manually delete the first couple of lines so that the very first character in the file is one of these unusual characters, then everything in the file is read perfectly. The unusual characters obviously just display as little ascii squiggles -arrows, smiley faces etc, which is fine. It seems as though a decision is being made automatically, without my knowledge or consent, based on the first line read.
Based on some googling, I suspected that the issue might be with the locale, but according to the visual studio debugger, the locale property of the ifstream object is "C" in both scenarios.
The code which reads the data is as follows:
//Function to open file at location specified by inFilePath, load and process data
int OpenFile(const char* inFilePath)
{
string line;
ifstream codeFile;
//open text file
codeFile.open(inFilePath,ios::in);
//read file line by line
while ( codeFile.good() )
{
getline(codeFile,line);
//check non-zero length
if (line != "")
ProcessLine(&line[0]);
}
//close line
codeFile.close();
return 1;
}
If anyone has any suggestions as to what might be going on or how to fix it, they would be very welcome.
From reading about your issues it sounds like you are reading in binary data, which will cause getline() to throw out content or simply skip over the line.
You have a couple of choices:
If you simply need lines from the data file you can first sanitise them by removing all non-printable characters (that is the "official" name for those weird ascii characters). On UNIX a tool such as strings would help you with that process.
You can off course also do this programmatically in your code by simply reading in X amount of data, storing it in a string, and then removing those characters that fall outside of the standard ASCII character range. This will most likely cause you to lose any unicode that may be stored in the file.
You change your program to understand the format and basically write a parser that allows you to parse the document in a more sane way.
If you can, I would suggest trying solution number 1, simply to see if the results are sane and can still be used. You mention that this is medical data, do you per-chance know what file format this is? If you are trying to find out and have access to a unix/linux machine you can use the utility file and maybe it can give you a clue (worst case it will tell you it is simply data).
If possible try getting a "clean" file that you can post the hex dump of so that we can try to provide better help than that what we are currently providing. With clean I mean that there is no personally identifying information in the file.
For number 2, open the file in binary mode. You mentioned using Windows, binary and non-binary files in std::fstream objects are handled differently, whereas on UNIX systems this is not the case (on most systems, I'm sure I'll get a comment regarding the one system that doesn't match this description).
codeFile.open(inFilePath,ios::in);
would become
codeFile.open(inFilePath, ios::in | ios::binary);
Instead of getline() you will want to become intimately familiar with .read() which will allow unformatted operations on the ifstream.
Reading will be like this:
// This code has not been tested!
char input[1024];
codeFile.read(input, 1024);
int actual_read = codeFile.gcount();
// Here you can process input, up to a maximum of actual_read characters.
//ProcessLine() // We didn't necessarily read a line!
ProcessData(input, actual_read);
The other thing as mentioned is that you can change the locale for the current stream and change the separator it considers a new line, maybe this will fix your issue without requiring to use the unformatted operators:
imbue the stream with a new locale that only knows about the newline. This method may or may not let your getline() function without issues.

C++ standard output format

I want to create a C++ console application that print some text to different parts of the console. For example in QBasic you can use:
locate(8,5)
print "hi"
And hi would be printed in column 8 line 5. In C++ when I use cout it always prints on the next line, and begins printing in the first column.
Is there any way I can do this?
C++ itself does not have this feature, it's I/O model is a fairly simple, sequential one.
If you want to do fancy cursor positioning, you'll need to output (for example) control characters which your terminal will recognise as special commands (such as ANSI or VT escape sequences), or use a library like curses (see ncurses here) which can do a lot of the grunt work for you, not just cursor positioning but also things like text mode windows and so forth.
A library, like ncurses can help you do this.