-
File content -> string?
Is there a way to get the contents of a file and place it into a string?
So far I have the following:
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
int main()
{
ifstream file;
string word;
int count;
map<string, int> table;
file.open("main.cpp");
if(!file)
{
return 1;
}
word = "#include";
system("PAUSE");
return EXIT_SUCCESS;
}
I've tried setting word = file, doing a while(file>>word), and some other things (such as setting word = file.open(), file.count, and things that generally didn't work).
Is there a way to set the contents from that file into the string for manipulation?
-
try somethign along these lines:
Code:
#include<iostream>
#include<fstream>
#include<string>
int main()
{
std::string word;
std::fstream file("Untitled1.dat",std::ios::in);
getline(file,word,static_cast<char>(0x3));
std::cout<<word;
std::cin.get();
return 0;
}
in case you're wondering, 0x3 is the hex value for ETX, or End of Text. I don't know how reliable it is, but it worked in the case I tried it in.
-
THANK YOU!
Now why can't google pop up with stuff like this? ><
-
read the first hit, second block of example code: http://www.google.com/search?num=100...2B&btnG=Search
:D
-
Figures I would search for the wrong thing...
Anyway, I hate to be a bother but is there any speacific reason why "no" always returns as 0?
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string search = "#include";
std::string word;
vector<string> vs;
//int no= 0;
std::fstream file("main.cpp",std::ios::in);
while(getline(file,word,static_cast<char>(0x3)))
{
vs.push_back(word);
}
int no = count(vs.begin(),vs.end(),search);
cout<<word;
cout<<no;
std::cin.get();
return EXIT_SUCCESS;
}
As far as I can tell I'm using the count() functions correctly, and there is obviously more than 0 #includes in the string...
Bleh ><
-
getline() reads in a line up to a newline, not just any white space, so vs[0] would be "#include <cstdlib>" instead of just "#include". I suggest using the >> operator rather than getline(). Alternatively, you could use a function search through the strings for "#include".
-
I'm sorry, joshdick, but I'm not sure what you are saying to do. Could you "dumb" it down a bit?
EDIT: I managed to get it to work on a per line basis, but I can't find a way to search for a substring (so I can search for include instead of #include....)
-
If you have code like
getline(cin, name);
and I enter "John Q. Public" and hit enter
then name == "John Q. Public".
But if instead you have code link
cin >> name;
and I enter "John Q. Public" and hit enter,
only "John" will be stored in name.
getline() keeps reading in until a newline (or EOF) is reached.
cin >> name; only reads up to whitespace.
If you'd prefer to store one word at a time without whitespace, use cin >>. If you want to store an entire line with spaces and tabs and such in it, then use getline(). I think for your purposes it'd be better to do the former.
-
Code:
string a = "Hello, World!";
cout << a.find("lo");
This will output 3 because that is the index of the substring "lo" in a.
-
Meh...works good enough (the number of includes was just something I wanted to add...), just wish I could get it to stop right before "using"...
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string search = "using";
std::string word;
vector<string> vs;
int no= 0;
std::fstream file("main.cpp",std::ios::in);
while(getline(file,word,'{'))
{
vs.push_back(word);
}
cout<<"Showing all includes, using namespaces, and arguments of main"<<'\n';
cout<<vs[0]<<"\n";
std::cin.get();
return EXIT_SUCCESS;
}