Passing full path as argument when it contains variable strings


 
Thread Tools Search this Thread
Top Forums Programming Passing full path as argument when it contains variable strings
# 1  
Old 06-24-2012
Passing full path as argument when it contains variable strings

Hi,

In directory "inoutfiles", I have folders fold0001, fold0002 and so on. Every folder has corresponding file file0001.txt, file0002.txt and so on. I want to perform a certain action on multiple files in one go. The cpp file is in the same directory as "inoutfiles".

This is my code :
Code:
char fold[30], file[30], pfold[20] = {'i','n','o','u','t','f','i','l','e','s'};
string str1 = "fold", str2 = "file", ext = ".txt", pf = "inoutfiles";
int size1, size2;
string padnum, index, foldername, filename;
for(int i=10005; i<10010; i++)
{
stringstream ss;
ss << i;
index = ss.str();
padnum = index.substr(1,index.length()-1);
foldername = str1 + padnum;
filename = str2 + padnum + ext;
size1 = foldername.size();
for(int j=0; j<=size1; j++) fold[j]=foldername[j];
size2 = filename.size();
for(int k=0; k<=size2; k++) file[k]=filename[k];
f.open(pfold/fold/file);
...
}

The error is operator '/' is binary. How to get around this?

Also, f.open((pf.c_str())/(foldername.c_str())/(filename.c_str())) is not working.

Last edited by radoulov; 06-25-2012 at 06:31 PM.. Reason: code tags
# 2  
Old 06-27-2012
Hi,

My friend pointed out the mistake in my code and I'm posting the correction here. It was so easy yet I don't know how it was overlooked.

Outside loop :

Code:
string path = "inoutfiles";

Inside loop :

Code:
path = path + "/" + foldername + "/" + filename;
f.open(path.c_str());


Last edited by KidD312; 06-27-2012 at 06:29 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Identify full path in argument

I have a small script to send copies of files to another computer used for tests but in the same location:pwd=`pwd` for i in "$@" do echo "rcp -p $i comp-2:$pwd/$i" rcp -p $i comp-2:$pwd/$i echo "Finished with $i" doneIs there a way I can check the parameter to see if it is a full... (5 Replies)
Discussion started by: wbport
5 Replies

2. Shell Programming and Scripting

Passing variable as an argument to another script

Hi, I am trying to pass a variable as an argument to another script. While substitution of variable, I am facing a problem. varaiable "a" value should be -b "FPT MAIN". When we pass "a" to another script, we are expecing it to get substitue as ./test.sh -b "FPT MAIN". But, it is getting... (9 Replies)
Discussion started by: Manasa Pradeep
9 Replies

3. Shell Programming and Scripting

Argument passing

How to pass the alphabet character as a argument in case and in if block? ex: c=$1 if a-z ]] then echo "alphabet" case $1 in a-z) echo "the value is a alphabet" edit by bakunin: please use CODE-tags. We REALLY mean it. (9 Replies)
Discussion started by: Roozo
9 Replies

4. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. UNIX for Advanced & Expert Users

Passing full path as argument when it contains variable strings

Hi, In directory "inoutfiles", I have folders fold0001, fold0002 and so on. Every folder has corresponding file file0001.txt, file0002.txt and so on. I want to perform a certain action on multiple files in one go. The cpp file is in the same directory as "inoutfiles". This is my code : ... (0 Replies)
Discussion started by: KidD312
0 Replies

6. Shell Programming and Scripting

How to extract strings from full path when full path is not fixed

/Path/snowbird9/nrfCompMgrRave1230100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird6/nrfCompMgrRave1220100920.log.gz:09/20/2010 06:14:51 ERROR Error Message. /Path/snowbird14/nrfCompMgrRave920100920.log.gz:09/20/2010 06:14:51 ERROR Error Message.... (0 Replies)
Discussion started by: Shirisha
0 Replies

7. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

8. Shell Programming and Scripting

passing Argument

Hi All, i have script like below.. echo "1) first option" echo "" echo "2) second option" echo "" echo "*) please enter the correct option" read select case $select in 1) echo "first option selected" ;; 2) echo "second option selected" ;; *) echo "please enter the correct... (4 Replies)
Discussion started by: Shahul
4 Replies

9. Shell Programming and Scripting

getting full path from relative path

given a relative path, how do i convert it into a full one. i.e. if i am in /home/polypus and i am given foo/bar then to get a full path i can just concatinate it with pwd, but what if i am given "../mama" how do i programmatically convert: /home/polypus and ../mama into ... (4 Replies)
Discussion started by: polypus
4 Replies

10. Shell Programming and Scripting

Passing the command line argument in a variable

Hi, I am new to unix. Is their a way to pass the output of the line below to a variable var1. ls -1t | head -1. I am trying something like var1=ls -1t | head -1, but I get error. Situation is: I get file everyday through FTP in my unix box. I have to write a script that picks up first... (1 Reply)
Discussion started by: rkumar28
1 Replies
Login or Register to Ask a Question