[C]C编程注意事项之linux环境下

一般的,在gcc编译器环境下

1. 头文件的注意
象conio.h应该变成curses.h

2. 注意itoa函数
标准C/C++里没有itoa函数。在Windows平台下一些编译器提供了该函数,但缺乏移植性

3. 关于sqrt,sin和cos函数
在引入math.h头文件后的情况下,也会存在找不到sqrt,sin和cos函数,用链接库可以解决

4. Makefile出现missing separator的错误
在目标声明行下面的命令行必须用Tab键分开,而不是空格

5. 留意文件的DOS格式字符
调试的时候须留意文件可能有DOS格式字符,须先去掉
tr -d <file> tmp_file
mv tmp_file <file>

阅读全文 »

02月08

[C]获取目录下面的文件名

#include <stdio.h>/*输入输出头文件*/
#include <dirent.h> /*LINUX系统下的一个头文件,gcc下的,win环境需要写相应的移植代码*/
#include <string.h>

typedef struct ListFileName
{
char filename[64];
struct ListFileName *next;
}FILENODE;

FILENODE* getFileName(char *dir)
{
DIR *directory_pointer;
struct dirent *entry;
directory_pointer=opendir(dir);
struct ListFileName start;
struct ListFileName *filesNode;
start.next=NULL;
filesNode=&start;
while ((entry=readdir(directory_pointer))!=NULL)
{
filesNode-> next=(struct ListFileName *)malloc(sizeof(struct ListFileName));
filesNode=filesNode-> next;
strcpy(filesNode-> filename,entry-> d_name);
filesNode-> next=NULL;
}
closedir(directory_pointer);
filesNode=start.next;
return filesNode;
}

int main()
{
struct ListFileName *filesNode;
char dir[100];
scanf("%s", dir);
...

阅读全文 »

02月08