Read multiple strings input using fgets() - c++

I need to
Take a command line argument giving number of strings (say N).
Call a function to take N input lines from user (using fgets)
Store them in an array of pointers.
i.e. char *input_lines[MAX_LINES];
All newlines from the lines should be removed
How can I achieve this?

Related

Reading series of chars without arrays or strings

i am struggling with one problem in my program. For input I am given ID that is series of 3 upper case letters, like ABC.
The problem is I have to read them somehow without using any other header file than <iostream> and I cannot use string nor any array types, such as char[] or string[]. How can I read series of multiple letters? Can I split them somehow into three chars?
The ID is needed later for implementation of the list - the three letters is an ID of a specific list and there will be also other lists, and the program basically will have to perform some operations on them such as adding elements, merging etc.
Thanks you for your help!
Strange requirements but just read them one at a time into separate variables.
char a = cin.get();
char b = cin.get();
char c = cin.get();
get reads a single character.
U can use scanf() or printf() methods.

Distinguishing between single and multiple characters while using get()?

The get() function is used to read a single character and to read multiple characters into an array. How does the system know if it is to read a single character or multiple characters?
Based on the parameters you specify, it will direct the calls to the matching prototype of the function.
For example if you call get(character reference) it will consider that one character has to be read from the stream.
If you pass a string (pointer) as argument and specify the length, it will consider reading characters in stream uptil that length in get(string, stream length).
If you specify a delimiter (add a 'delimiter character' to the parameters above) it will read uptil that character is matched against.
If you specify a stream-buffer using get(streambuf) it will read all the available characters. (can add a delimiter here as well)
Evident from the above, only the first prototype get(character reference) or get(char& c) reads a single character, rest read multiple characters (eg: passing a string/stream-buffer with length), and this is known by the parameters you provide or the function prototype you follow during your call to get(). Reference

C++ Fastest way to parse through a file

I have a file which I have opened using:
ifstream ifile(FilePath)
The file contains, say 10 lines of data and each line contains an evenly-incrementing number of comma separated values (like a pyramid). So first line has 1 value, second line has 2 values and so on....
I wanted to do the following, all within one function (whilst traversing the file char array just the once):
-Every time I encounter a newline character, I can increment a parameter passed in by value (which, when the function exits, I have the number of lines in the file).
-I also wanted to store each line of the file in an array. What would be the best way to "glue" together all the characters between newline characters?
I'd prefer to use statically-allocated arrays, but the problem is I only know the size of the array once I have performed step 1 (counting how many new line characters there are). Would it be quicker to perform a double-parse for this (one parse to count how many lines, then use that value to allocate a static array) or single parse, but insert into a dynamic array?
The emphasis on this is to write fast code (so not being OO-friendly is not a concern)
Apologies if I am asking a lot, hopefully you can see I have given this some thought.
EDIT, example file:
a
b,c
d,e,f,g,h
j,k,l,m,n,o,p
From this file I would want to achieve:
Knowledge that there are 4 lines in the file
A non-dynamic array containing each line
The number of elements in the second line
There are plenty of examples in existing posts on how to do this.
if you want to use ifstream to read in the file once, you can do something like this:
std::ifstream in("myfile");
std::stringstream buffer;
buffer << in.rdbuf();
std::string contents(buffer.str());

passing table of floats as argument in c++ main

I have a c++ program, I would like the first argument of the main (argv[1]) to correspond to a table of float. Is it possible to do that??
I was thinking about putting in a string my floats separated with spaces (e.g. "1.12 3.23 4.32 1.1 ...")
Is there a way to automatically convert such a string into a table of floats? If I understand well the atof function converts a string into a double. So it seems it could be possible to split my string using the spaces and then convert each portion using atof.
This option does not seem to be very efficient to me? In addition it returns double and not float :(
So, is there a better way to pass table of float as argument of a c++ program ?
Thank you
A stringstream can do both the splitting at spaces and the parsing into a float.
std::stringstream ss(the_string);
std::vector<float> v(std::istream_iterator<float>(ss),
(std::istream_iterator<float>()));
// the extra parentheses here are ugly but necessary :(
How to obtain the string with the data depends on how large it is and where it is supposed to come from. Just keep in mind that in many systems the arguments passed to program are already split by spaces, putting each part in a different element of argv.
Save it in a text file, and then read it from the file when your program starts. I isn't worth it to pass it as a command-line argument.
The main() parameter list is as it is. You can pass the strings of your numbers as arguments to your program. The main function will have to parse its argument.
When you want to pass a space separated list of numbers in argv[1] you can use the strtok function to get the individual number strings and have to pass it to a conversion function.
When your conversion function returns a double you should check that the result can be represented by a float and cast the value to a float variable. But I would consider to use double as the internal representation.
In addition to Singer's answer:
The commandline should be used mainly by human, not computer. If you really need a table of values, use configuration file. You can always use human readable format for it.

fgets - maximum size (int num)

In my program, i'm calling the function fgets:
char * fgets ( char * str, int num, FILE * stream );
in a loop several times, and then deal with the new coming input (in case there is one).
in the fgets specifications, it says that:
num:
Maximum number of characters to be read (including the final
null-character). Usually, the length
of the array passed as str is used.
The problem is that i want to rean NO MORE than the specified num - and IGNORE the rest of it, if it's in the same line.
What i've found out, is that the fgets reads the next part of the line in the next call to the function.
How can i avoid this behavior?
You'll need to do it manually - consume the characters with fgets and copy the results to a result array until a newline character is found, dump the contents, and continue with fgets.
The size parameter is intended to be used to prevent reading more data than your buffer can hold. It won't work for skipping over data.
You'll have to write code to throw away the parts of the string you don't want after it's read.
fgets() is a old C function. The idea is that the language will provide minimal complexity functions that you can combine to do what you like. They don't include any extra capability on purpose. This keeps everyone from paying for things they don't use. Think LEGO.