Reading structured arguments


 
Thread Tools Search this Thread
Top Forums Programming Reading structured arguments
# 1  
Old 02-27-2014
Reading structured arguments

I am passing an argument to a C++ program which is going to look like

[12,34,5,6][12,4,5,6][34,2,4,6][2,1,4,6,][12,23,13,5]

I need to get the integers into arrays a, b, c, d with

a= 12,12,34,2,12
b= 34,4,2,1,23
c= 5,5,4,4,13
d= 6,6,6,6,5
# 2  
Old 02-27-2014
How flexible does this need to be -- are these all 1D arrays, is the number of arrays fixed?
# 3  
Old 02-27-2014
One thing I can do is that if I have a string with 4 numbers seperated by comma, I can place it in a vector n as follows

Code:
string s = "2,4,8,16";
vector<int> n;
transform(s.begin(), s.end(), [](char c){return c == ';' ? ' ' : c});
stringstream ss(s);
copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(n));

# 4  
Old 02-27-2014
How flexible does this need to be -- are these all 1D arrays, is the number of arrays fixed?
# 5  
Old 02-27-2014
Quote:
Originally Posted by Corona688
How flexible does this need to be -- are these all 1D arrays, is the number of arrays fixed?
They should all be in 4 elements [a,b,c,d][a,b,c,d]. We can also force the user to tell us how many sets he passed

program "[a1,b1,c1,d1][a2,b2,c2,d2][a3,b3,c3,d3][a4,b4,c4,d4]" etc

This will be put in a string using getoptlong.
# 6  
Old 02-27-2014
So, a variable number of arrays, then. OK.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
        char *input="[11,12,13,14][21,22,23,24][31,32,33,34][41,42,43,44]";
        // Used like a 2D array once it's populated.  arrarr[0][0] would be 11, arrarr[0][1] would be 12, arrarr[1][0] would be 21...
        int **arrarr=NULL;
        int row=-1, n;

        while(input != NULL)
        {
                arrarr=realloc(arrarr, sizeof(int *)*(++row+1));
                arrarr[row]=malloc(4*sizeof(int));

                if(sscanf(input, "[%d,%d,%d,%d]",
                        &(arrarr[row][0]), &(arrarr[row][1]),
                        &(arrarr[row][2]),&(arrarr[row][3])) != 4)
                {
                        fprintf(stderr, "Bad row:  %s\n", input);
                        exit(1);
                }

                input=strchr(input+1,'[');
        }

        for(n=0; n<=row; n++)
                printf("Row %d [%d,%d,%d,%d]\n",
                        n, (arrarr[n][0]), (arrarr[n][1]),
                        (arrarr[n][2]),(arrarr[n][3]));
}

Code:
$ ./a.out
Row 0 [11,12,13,14]
Row 1 [21,22,23,24]
Row 2 [31,32,33,34]
Row 3 [41,42,43,44]

$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading arguments for a shell script from file

I have a shell script that takes 2 arguments. I will have to execute this script multiple times with different values for the arguments. for example, ./shscript env1 value1 ./shscript env1 value2 ./shscript env2 value3 ./shscript env3 value4 ./shscript env1 value5 ./shscript env3... (24 Replies)
Discussion started by: goddevil
24 Replies

2. Programming

Reading command line arguments and setting up values if option not provided

I have a C++ program. I read command line arguments, but if the value is not supplied, I default or make a calculation. Let's say I set it to a default value. I can code this in several ways. Here I show three ways. What would be the best way for maintaining this code? The program will get very... (2 Replies)
Discussion started by: kristinu
2 Replies

3. Shell Programming and Scripting

Reading arguments but exclusing some

I have a tcsh script which I pass arguments to. I want to store all the arguments except for the options. Currently I am using the line below, however I want to also exclude and variation of them (e.g. whether any letter is uppercase or lower case) Is there a neat way to do it? set... (0 Replies)
Discussion started by: kristinu
0 Replies

4. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

5. Shell Programming and Scripting

Parsing structured files in Perl

Hi, looking for a piece of code to get the values from a structured text file like this: ############################################################################### ############################################################################### # # PERSONAL DATA DEFINITIONS #... (8 Replies)
Discussion started by: sarabande
8 Replies

6. Shell Programming and Scripting

structured file update

Hi I have a very structured file consisting of multiple lines as follows: 3752 AVAILABLE 06/24/2009 FFFF 000000 0000 0000 3753 TRADITION (ASIA) LTD TACB 008329 0000 0000 3754 WACHOVIA CONVS/PRFDS WBCP 001099 0000 0000 3755 AVAILABLE 05/12/2009 FFFF 000000 0000 0000 3756... (3 Replies)
Discussion started by: aoussenko
3 Replies

7. Shell Programming and Scripting

reading in arguments with spaces

I want to be able to read in input which contain spaces and put that into an array. Each field should be delimeted by a space and should be a different array element. However I cant get it to work. any tips? read input //type field1 field2 field3 echo "$input" array="$input" Thanks in... (11 Replies)
Discussion started by: Calypso
11 Replies

8. Shell Programming and Scripting

Bash: Reading 2 arguments from a command line

If no arguments are entered I wanna be able to read 2 arguments, i have done like this but it doesnt work: x=0 until #loop starts do if ; then echo No arguments were entered, please enter 2 arguments. read $1 $2 elif || ; then echo $#... (0 Replies)
Discussion started by: Vozx
0 Replies

9. UNIX for Dummies Questions & Answers

Reading runtime arguments from a file

Hi All, I am running a script which wud take a long time to complete execution ... In between, it asks for yes/no sort of confirmation from user for doing some acivities .... Now that I dont want to wait till the script executes upto this point where it needs user confirmation, is there any way... (4 Replies)
Discussion started by: Sabari Nath S
4 Replies

10. UNIX for Dummies Questions & Answers

Entering directories structured above user.

I wish to enter directories which are on the HD but not part of the user hierarchy and have access to the contents of an external HD. To an advanced user this is probably a stupid question but I experimented with ~, cd, . and.. to no avail. I did check for a suitable command but couldn't find one.... (9 Replies)
Discussion started by: Frank Hunter
9 Replies
Login or Register to Ask a Question