C++ \n in a string (Not working) [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm learning C++. Here's my problem.
http://prntscr.com/2m5flm
I created a function who can read Prop files like them (you can set the file beginning and ending tags with a function, searching a specified tag with a function who will return a string (containing the results).
Here's the main.cpp
#include <iostream>
#include "m_PFile_r.h"
using namespace std;
int main(int argc, const char * argv[])
{
m_PFile_r k;
k.open("prop.arg");
k.imNotaFag(true);
k.setOpenArg("$FILE_BEGIN$");
k.setCloseArg("$FILE_END$");
string lS;
lS=k.getArg("launchSentence");
cout << lS << endl;
string menu;
menu=k.getArg("progMenu");
cout << menu;
return 0;
}
MY QUESTION IS : Why doesn't it print the \n as a line return ?
Thanks :)

The file has new line characters in it, they are defining the end of the lines. When you enter the newline character in the file, it is being stored in that file not as a newline character, but the two individual characters "\" and "n". So when you then read in the file, those characters are read in just like the others.
You are over complicating this problem. If you would like to print out various phrases to the user, just include those phrases as string variables in your program.
String launchSentence = "This is the launch sentence.";
String progMenu = "Hit 1 For Add Hit 2 for Subtract";
These could then be printed with the normal COUT << progMenu method.
If your purpose with the text file is to keep all of the possible text strings isolated in one easy location, why not create a TextCommandPrompts.h, fill it with the String (character in C++) variables and include that in your main?
Edit - Because I can't comment yet and I want to respond to one - I thought that whatever text editor that was letting him write line by line would be messing this up. As in, its already doing the "\n" magic, and when he writes in the characters '\' and 'n' something mundane happened, and they stayed as regular characters.

Related

Remove blank line in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
How to remover blank lines after reading a text file using c++?
while(arry[i]="/n"){
i++;
}
This is my code.It gives some output.But it is not a expected output.
while (arry[i] == '\r' || arry[i] == '\n') {
i++;
}
And please add the checkpoint of the boundary of arry like that:
i < arry.length()
\r is a carriage return character; it tells your terminal emulator to move the cursor at the start of the line. So, \r allows to override the current line of the terminal emulator.
In C++ single quotes identify a single character, while double quotes create a string literal. 'a' is a single a character literal, while "a" is a string literal containing an 'a' and a null terminator

Replace a particular word with another one in a file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I find a particular word in text file and replace that word with another one and write back the test to file(If text is paragraph separated).I can do replace word; if its not paragraph separated .using string.find and string replace.
As far as I understand you are comfortable with replacing words in a single paragraph and that you are doubtful regarding text with multiple paragraphs.
Please look into a function called "getline()" function.
This function reads entire your text until it encounters a "\n" element (Next line)
So you can use this getline function to get one whole paragraph into a string.
Using this getline function in a while loop allows you to get all the paragraphs from the text file
An example code has been provided below
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string a,b;
a="he";
b="she";
fstream text("text.txt");
string line;
while (!text.eof( ))
{
getline(text,line);
cout<<line<<endl;
//This string "line" is basically a string containing your first paragraph
//ADD your find and replace code here for the string "line".
//The second time the while loop executes the string "line" will contain the second paragraph and so on..
}
}
}

Can not open a text file with the " fstream " [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My current difficulties will open "My File.txt" using fstream.
in "My File.txt" there is a long sentence. so I use the AnsiString to accommodate the text that is inside
void __fastcall TFormManager::Button1Click(TObject *Sender) {
AnsiString FileName, tmpText;
FileName = "\conf\db\My Text.txt";
if (FileExists(FileName)) {
ifstream data(FileName);
data >> tmpText;
}
}
i use C++ Builder XE6. Thx
Double the backslashes:
FileName = "\\conf\\db\\My Text.txt";
Sometimes you need to put a symbol in a string literal that has no equivalent in the keyboard or that can not be accepted directly in the source code. For example, if you need to add a line break in a string, many languages require you to use an equivalent escape sequence.
In C/C++, escape sequences are started by a single \. So for example:
\n = Enter
\t = Tab
If you want to use a single \ in your string literal, you must escape it as \\.
http://en.wikipedia.org/wiki/Escape_sequences_in_C
In many systems, you can use / to separate your path.

C++ -- Storing from .txt file to array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
2 Questions:
The first question is, I need to input the name of the text file when the program runs so let's say it's called "Banana.txt" , I type it in and I'm missing the last word of the file. This is basically all I used to output from the txt file.
while(File.good()){
cout <<word << " "; //put spaces in between the words
File >> word; }
Not sure how to get the last word.
The second question is how do I store the information from the .txt file into an array that I can use later?
Does it have to be multidimensional? The maximum words in the file are 100..
Eventually I'm going to need to ignore any words < 4 characters.
I actually can't recall if I should use char or string. But iirc char is each individual character whereas string is a collection of characters? Arg. Scratch that, Looking it up in a bit..
Not allowed to use hash_, vectors, maps, stack, or lists so I'm not sure how to go about this problem
Thanks in advance for the help. I tried looking through other threads but I'm not sure if those are the ones I'm looking for... Sorry for the questions ..
You're outputting before you've input.
Try swapping your two lines in the while loop, something like:
while(File.good())
{
File >> word;
cout << word << " "; //put spaces in between the words
}
to put the data into a container, assuming word is of type string, try:
vector<string> v;
and at the bottom of your loop:
v.push_back(word);
For the first one, you need to first read into the word then output it.
The "intuitive" loop of while(File.good()) though is flawed because if you are at end of file but haven't read it yet, the stream will appear good until you try reading the next word.
As a result the read will fail. You could do
while( File.good() )
{
if( File >> word )
{
// process this word
}
}
but simpler is
while( File >> word )
{
// process the word
}
Another thing you can use is istream_iterator and copy, and then you can actually copy them into your sequence. However you are not allowed to use the standard C++ library.

Is there any way to extract URL from text in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In PHP I can write regex to extract URL from the text.
Wanted to know any such class or method available in C++?
I am working with streaming data, which may contains URLs. I want to extract each URL from that with there count value.
I can use vector or other data structure for later processing but question is with title.
C++11 introduced <regex> as part of the standard library.
Let's take a look at how to use it.
First we need to import the header.
#include <regex>
Now let's declare our URL regex. For now we'll use something very simple. I'll leave it up to you to replace it with a more complete regex. Notice how we use \\ instead of just \ to escape things. \ itself is a special character in C++ so we need to escape it.
std::regex url(".*\\..*");
Let's create a string to test this against.
std::string url_test = "example.com";
Now let's check if url_test matches url and print out a message accordingly.
if(regex_match(url_test, url)) {
std::cout << "It's a url!" << std::endl;
} else {
std::cout << "Oh snap! It's not a url!" << std::endl;
}
Our complete program:
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::regex url(".*\\..*");
std::string url_test = "example.com";
if(regex_match(url_test, url)) {
std::cout << "It's a url!" << std::endl;
} else {
std::cout << "Oh snap! It's not a url!" << std::endl;
}
}
Read more at http://www.cplusplus.com/reference/regex/
In regards to a regex, I use the following to match a multitude of links:
\b(?:(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&##/%?=~_|$!:,.;]*[-A-Z0-9+&##/%=~_|$]
| ((?:mailto:)?[A-Z0-9._%+-]+#[A-Z0-9._%-]+\.[A-Z]{2,4})\b)
|"(?:(?:https?|ftp|file)://|www\.|ftp\.)[^"\r\n]+"?
|'(?:(?:https?|ftp|file)://|www\.|ftp\.)[^'\r\n]+'
This allows matching of web links with and without http/https at the beginning, email links with and without a mailto at the start, ftp links and file links, as well as links within single or double quotes.
I have not used the regex functionality of C++ (<regex> ) however shall have a look at it today and get back to you with some code exemplars.