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

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.

Related

How to read in a txt file in c++, if we don't know how many data strings are in it, and how long each row is? [duplicate]

This question already has answers here:
Read file line by line using ifstream in C++
(8 answers)
Closed 5 years ago.
There is an input.txt, with string sorted like this:
Name1 Name2 "number1:number2" "number1:number2"...
.
.
.
NameX NameY "number1:number2" " number1:number2"...
Baically we don't know how much names are in it, and how much "number:number" strings are in a row. The task is to write out the first name in each line, and write out the number of times, when number1 is bigger than number2 in each row.
The thing is, I don't know how should I read in the pairs, without knowing how many are there from them in each row.
Thanks for the help in advance.
edit: My problem is not actually the part where I don't know how long the file is, I can read it in line by line, I don't know how should I read in a row, where I don't know how many strings are.
edit2, exaple: Steven Jack 2:5 6:4 7:2
You could get the seperated elements with a stringstream one by one, and check if it starts with a decimal digit character or not (works only if there isn't any name that starts with a number).
like so:
std::string line; // assuming you got the line
std::stringstream ss(line);
std::string str;
while (ss >> str) {
// use isdigit() to check if it's a decimal digit character or not
if (isdigit(s.at(0))) {
// do something with NUMBERS
// getting the actual numbers from these strings is another problem
} else {
// do something with NAMES
}
}
EDIT : as you said getting lines one by one is not a problem, so the "line" variable in my code assumes that you somehow got a whole line in the memory and works with this line.
EDIT2 : it's not much different if you don't have quotation marks, in this case, you check if the first character is a number or not (btw if any name starts with a number it's gonna be a problem)

I cannot read spaces and enters in c++ using devc++ [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 6 years ago.
Improve this question
Hi I am new here and I have little problem I guess. I want my program to change one character in whole text in text file. For example I want to change all A to G. But when I read it ,it does not even read spaces and enters. Also I want to write eddited text in new textfile. Thanks for help.
At the beginning you should load whole file.
std::ifstream is("file.txt");
is >> std::noskipws;
std::istream_iterator<char> start(is), end;
std::vector<char> buffer(start, end);
is >> std::noskipws prevents stream from skipping white characters like space or enter. Then replace characters using std::replace(buffer.begin(), buffer.end(), 'A', 'G');.
Now simply save file:
std::ofstream out("file.txt", std::ofstream::binary);
std::copy(buffer.begin(), buffer.end(), std::ostreambuf_iterator<char>(out));
You may want to read something about those:
std::ostreambuf_iterator, std::istream_iterator, std::noskipws, std::replace.

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

How to insert data in sorted file in C++? [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 8 years ago.
Improve this question
I need to insert data in file in C++. I need to insert First and Last Name. All the lines in the file are sorted by the Last Name first, and then by the First Name. So my problem is when i insert new line how to insert on the right place and all the records still be sorted?
With this code i just append data in file, but don't know how to sort
`
string fName,lName;
cout<<"Insert Last Name: "<<endl;
cin>>lName;
cout<<"Insert First Name: "<<endl;
cin>>fName;
ofstream myfile("sort.dat",ios::app);
myfile<<fName<<" "<<lName;`
There are multiple things working even with the code you posted:
You need to check if your input was successful before processing any input. Otherwise you could, e.g., end up with empty strings being inserted into your file, probably corrupting its content.
You almost certainly need to put a newline into the target file when appending the new content.
That is, your input could, for example, look like this:
std::string fName, lName;
if (std::cout << "Insert last name: "
&& std::cin >> lName
&& std::cout << "Insert first Name: "
&& std::cin >> fName) {
// now do something with the read values
}
else {
// deal with the error
}
The next step is "insert into a file": that doesn't work. Sorting a file is bound to work even less! Instead, what you would copy the file to a new destination and insert the record at the opportune location
Read each line of the existing file. If the record is smaller than the new record, write it and go to line 1.
Write new record.
Write the already read record.
Copy the remaining content of the original file.
Close both files.
Remove the original file and rename the new file to the name of the original file.
I could type out the code but I don't really need exercise with reading/writing files.

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

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.