-5

I have a small assignment which partly requires me to take inputs from a file in the form of strings and place them into char arrays so I can check if the string contains any '*' character at the end of it.

I have been able to extract the strings from the files successfully, however i have failed to find a way in which to place them in char arrays so i can process them.

I would be very grateful if someone would let me know how to place a string into char arrays using cstring library. Please keep in mind that the strings are taken from a file and not as user input.

some of the ways i tried is the following:

//Try 1
char CstringArray[] = LineFromFile;

//Try 2
char CstringArray[100] = LineFromFile;

//Try 3
ifstream Test("Test.txt");
Test>>CstringArray;

//Try 4
ifstream Test("Test.txt");
Test>>CstringArray[0];

Thank you very much

BlacKnight BK
  • 119
  • 4
  • 12
  • Hello and welcome to SO. Please take [the tour](http://stackoverflow.com/tour) and read the [help page](http://stackoverflow.com/help). Here is a nice list of [C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). –  Jan 03 '17 at 10:01
  • It seems like i am always asking the wrong questions, can you help me @RawN understand what i am doing wrong that i keep on getting negative reputation and people not answering my questions? Thank you – BlacKnight BK Jan 03 '17 at 12:49
  • First there is no question in the above text. If this is an assignment then you are expected to provide what you have tried so far. The above example might seem trivial and maybe the dvoters expect you to google that part. Ask specific questions, provide a MCVE, follow guidelines. Don't worry to much about the downvotes, we all went through it (and still are). –  Jan 03 '17 at 12:59
  • @RawN Thank you, is that better now? – BlacKnight BK Jan 03 '17 at 14:45

1 Answers1

0

Since this is an assignment, your professor will probably not be happy with you using all of C++'s functionality, particularly if you don't understand it, but since it's a one liner I figured I'd tell you how I'd print all strings ending in an asterisks. Given that you have successfully opened the file to ifstream Test you can do:

copy_if(istream_iterator<string>(Test), istream_iterator<string>(), ostream_iterator<string>(cout, " "), [](const auto& i) { return !empty(i) && i.back() == '*'; })

EDIT:

  1. I'm using an istream_iterator to read in each string in Test and istream_iterator, I'm operating on these values immediately, but if you needed to start by saving all the strings to a vector<string> you could also do this: vector<string> CstringArray{ istream_iterator<string>(Test), istream_iterator<string>() }
  2. I'm using an ostream_iterator to directly stream out my selected strings rather than storing them
  3. I'm using copy_if to iterate over all the strings that are streamed in, selecting only those that meet a given criteria
  4. I'm using the lambda: [](const auto& i) { return !empty(i) && i.back() == '*'; } to conditionally select non-empty strings which end with an asterisks character
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • You are right he would not be happy if i used this, however i would still like to understand it if you would be kind enough to explain it to me, i have tried to figure it out myself and manipulate it a bit with little success. – BlacKnight BK Jan 03 '17 at 15:34
  • @BlacKnightBK I've added some links to the C++ tools I'm using to accomplish this. If you want to understand them you'll need to look into the documentation. If you can take the time to understand how these work you should be able to solve your problem using the primitive methods your instructor probably expects. – Jonathan Mee Jan 03 '17 at 15:55
  • Thanks alot :) This does help however will take me some time to figure out – BlacKnight BK Jan 03 '17 at 17:48