Sign-up....

字符串问题

/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"

* by Nicolai M. Josuttis, Addison-Wesley, 1999

*

* (C) Copyright Nicolai M. Josuttis 1999.

* Permission to copy, use, modify, sell and distribute this software

* is granted provided this copyright notice appears in all copies.

* This software is provided "as is" without express or implied

* warranty, and with no claim as to its suitability for any purpose.

*/

#include <iostream>

#include <string>

using namespace std;

int main (int argc, char* argv[])

{

string filename, basename, extname, tmpname;

const string suffix("tmp");

/* for each command-line argument

* (which is an ordinary C-string)

*/

for (int i=1; i<argc; ++i) {

// process argument as file name

filename = argv[i];

// search period in file name

string::size_type idx = filename.find('.');

if (idx == string::npos) {

// file name does not contain any period

tmpname = filename + '.' + suffix;

}

else {

/* split file name into base name and extension

* - base name contains all characters before the period

* - extension contains all characters after the period

*/

basename = filename.substr(0, idx);

extname = filename.substr(idx+1);

if (extname.empty()) {

// contains period but no extension: append tmp

tmpname = filename;

tmpname += suffix;

}

else if (extname == suffix) {

// replace extension tmp with xxx

tmpname = filename;

tmpname.replace (idx+1, extname.size(), "xxx");

}

else {

// replace any extension with tmp

tmpname = filename;

tmpname.replace (idx+1, string::npos, suffix);

}

}

// print file name and temporary name

cout << filename << " => " << tmpname << endl;

}

}

这是c++ standard library 中字符串的第一个例子,我不明白main传的两个参数是用来做什么的?

后面的char* 因该是指向输入字符串的,前面的int是用来干吗的呢?

请教各位高手

[2220 byte] By [msdn] at [2007-8-14 12:57:47]
# 1 Re: 字符串问题

10.1放假回来结贴大家国庆快乐

arden1019 at 2004-9-30 15:59:31 >
# 2 Re: 字符串问题

argc表明所传参数的个数

zjpangxie at 2004-9-30 16:03:29 >
# 3 Re: 字符串问题

能不能清楚一点说? 它是怎么工作的?

我在编译的时候,编译器怎么知道这个是用来传递参数个数的?还是有什么规则,手头没有资料,请帮忙解释一下.我如何来判断参数个数呢?读空格(谁帮我实现的这个功能)?

比如我编译后写:

string1.exe abc.exe cdf.fda fdsa fdfe.tmp 111..

它如何处理这些参数?来区分这5个参数呢?

arden1019 at 2004-9-30 16:13:51 >
# 4 Re: 字符串问题

如果写string1.exe abc.exe cdf.fda fdsa fdfe.tmp 111,那么

argc = 6

argv[0]="string1.exe"

argv[1]="abc.exe"

argv[2]="cdf.fda"

argv[3]="fdsa"

argv[4]="fdfe.tmp"

argv[5]="111"

zjpangxie at 2004-9-30 16:33:45 >
# 5 Re: 字符串问题

结了吧,以后再说.

arden1019 at 2004-10-6 13:20:38 >

C/C++

All Classified