有关 main(int argc,char** argv) 的问题

laiwing 2008-07-26 02:56:37

class stack
{..
};

int main(int argc,char** argv)
{
char buf[100];
stack textlines;

FILE* file = fopen(argv[1],"r");
while (fgets(buf,100,file))
{
char* string = (char*)malloc(100);
strcpy(string,buf);
textlines.push(string);
}

char* s;
while ( (s = (char*)textlines.pop() ) != 0)
{
wprintf(L"&s",s);
free(s);
}
}



FILE* file = fopen(argv[1],"r");
这是什么意思??自己读自己的档案???
...全文
135 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jerrykinki 2008-07-26
  • 打赏
  • 举报
回复
“要是我直接打开该文挡 1.exe 程序就会意常中止”
当然会这样!

直接运行
while (fgets(buf,100,file))
{
char* string = (char*)malloc(100);
strcpy(string,buf);
textlines.push(string);
}会出现异常。
应该在FILE* file = fopen(argv[1],"r");之后判断文件打开是不是成功?
laiwing 2008-07-26
  • 打赏
  • 举报
回复
要是我直接打开该文挡 1.exe 程序就会意常中止

在编译时候,同时也有警告信息,

c:\users\john\desktop\thinking in c++\4\2.cpp(73) : warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 9.0\vc\include\stdio.h(237) : 請參閱 'fopen' 的宣告


1>c:\users\john\desktop\thinking in c++\4\2.cpp(77) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 9.0\vc\include\string.h(74) : 請參閱 'strcpy' 的宣告

这是我的形程序码


#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

class stack
{
struct link
{
void* date;
link* next;
void initialize (void* const Date,link* const Next) { date = Date;next = Next;}
} *head;
public:
stack() { head = 0 ; }
~stack();
void push (void* const Date);
void* const peek()const { return head->date ; }
void* const pop();
};


void stack::push(void* const Date)
{
link* newlink = (link*)malloc(sizeof(link));
newlink->initialize(Date,head);
head = newlink;
}

void* const stack::pop()
{
if ( head == 0 )
return 0;
link* temp = head;
void* result = head->date;
head = head->next;
free(temp);
return result;
}

stack::~stack()
{
link *temp = head;
while ( temp != 0 )
{
head = head->next;
free(head->date);
free(head);
}
}

int main(int argc,char* argv[])
{
char buf[100];
stack textlines;

FILE* file = fopen(argv[1],"r");
while (fgets(buf,100,file))
{
char* string = (char*)malloc(strlen(buf)+1);
strcpy(string,buf);
textlines.push(string);
}

char* s;
while ( (s = (char*)textlines.pop() ) != 0)
{
printf("%s",s);
free(s);
}
}

e_sharp 2008-07-26
  • 打赏
  • 举报
回复
argv[1] 代表命令行传来的第一个参数

argv[0] 代表自己
hiksa 2008-07-26
  • 打赏
  • 举报
回复
晕死,我才发现这是我第一个知道为什么的问题。。。
同意楼上各位的
isikawamoeko 2008-07-26
  • 打赏
  • 举报
回复
比如你运行程序输入的命令是main.exe 1.txt
那读的是1.txt。
机智的呆呆 2008-07-26
  • 打赏
  • 举报
回复
argv[0]才是自己
xkyx_cn 2008-07-26
  • 打赏
  • 举报
回复
argv[1]是传进来的命令行参数

例如你在命令行里键入:
yourexe yourfile
你的程序就能读到2个命令行参数,都放在字符串数组argv里
argv[0] == "yourexe"
argv[1] == "yourfile"

这样就可以通过命令行参数来指定读取的文件了

33,318

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧