c语言文件读写“追加”

使用fseek()定位到相应的位置,使用fread()、fwrite()读写文件即可。下面是一个在1.txt的文件末尾追加hello world的示例:

c语言append是什么意思 c语言中end是什么意思c语言append是什么意思 c语言中end是什么意思


c语言append是什么意思 c语言中end是什么意思


#include

#include

int main()

{FILE fp = fopen("1.txt", "a+");

if (fp==0) { printf("can't open file

"); return 0;}

fseek(fp, 0, SEEK_END);

char sz_add[] = "hello world

";

fwrite(sz_add, strlen(sz_add), 1, fp);

fclose(fp);

return 0;

}

c语言!wt+ at+ rb+ 这里的+到底是什么意思??怎么用wt 与wt+的结果一样呢?文件!

读写的权限不一样。

r 以只读方式打开文件,该文件必须存在。

r+ 以可读写方式打开文件,该文件必须存在。

rb+ 读写打开一个二进制文件,允许读写数据,文件必须存在。

w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。

w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。

a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)

a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。

(原来的EOF符不保留)

wb 只写打开或新建一个二进制文件;只允许写数据。

wb+ 读写打开或建立一个二进制文件,允许读和写。

ab+ 读写打开一个二进制文件,允许读或在文件末追加数据。

at+ 打开一个叫string的文件,a表示append,就是说写入处理的时候是接着原来文件已有内容写入,不是从头写入覆盖掉,t表示打开文件的类型是文本文件,+号表示对文件既可以读也可以写。

上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,加入b

字符用来告诉函数库以二进制模式打开文件。如果不加b,表示默认加了t,即rt,wt,其中t表示以文本模式打开文件。

求c++大神给说下string中的append()函数的用法

append函数是向string 的后面追加字符或字符串。

(1)向string 的后面加C-string

basic_string& append( const value_type _Ptr );

basic_string& append( const value_type _Ptr, size_type _Count );

basic_string& append( const basic_string& _Str );

basic_string& append( const basic_string& _Str, size_type _Off,

template basic_string& append(

basic_string& append( size_type _Count, value_type _Ch );

参考:

要想使用标准C++中string类,必须要包含

#include

//

注意是,不是,带.h的是C语言中的头文件

using

std::string;

using

std::wstring;

或using

namespace

std;

下面你就可以使用string/wstring了,它们两分别对应着char和wchar_t。

string和wstring的用法是一样的,以下只用string作介绍:

string类的构造函数:

string(const

char

s);

//用c字符串s初始化

string(int

n,char

c);

//用n个字符c初始化

此外,string类还支持默认构造函数和构造函数,如string

s1;string

s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常

;string类的字符作:

const

char

&operator[](int

n)const;

const

char

&at(int

n)const;

char

&operator[](int

n);

char

&at(int

n);

operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。

const

char

data()const;//返回一个非null终止的c字符数组

const

char

c_str()const;//返回一个以null终止的c字符串

int

copy(char

s,

int

n,

int

=0)

const;//把当前串中以开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目

string的特性描述:

int

capacity()const;

//返回当前容量(即string中不必增加内存即可存放的元素个数)

int

max_size()const;

//返回string对象中可存放的字符串的长度

int

size()const;

//返回当前字符串的大小

int

length()const;

//返回当前字符串的长度

bool

empty()const;

//当前字符串是否为空

void

resize(int

len,char

c);//把字符串当前大小置为len,并用字符c填充不足的部分

string类的输入输出作:

string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出作。

函数getline(istream

&in,string

&s);用于从输入流in中读取字符串到s中,以换行符'n'分开。

string的赋值:

string

&operator=(const

string

&s);//把字符串s赋给当前字符串

string

&assign(const

char

s);//用c类型字符串s赋值

string

&assign(const

char

s,int

n);//用c字符串s开始的n个字符赋值

string

&assign(const

string

&s);//把字符串s赋给当前字符串

string

&assign(int

n,char

c);//用n个字符c赋值给当前字符串

string

&assign(const

string

&s,int

start,int

n);//把字符串s中从start开始的n个字符赋给当前字符串

string

&assign(const_iterator

first,const_itertor

last);//把first和last迭代器之间的部分赋给字符串

string的连接:

string

&operator+=(const

string

&s);//把字符串s连接到当前字符串的结尾

string

&append(const

char

s);

//把c类型字符串s连接到当前字符串结尾

string

&append(const

char

s,int

n);//把c类型字符串s的前n个字符连接到当前字符串结尾

string

&append(const

string

&s);

//同operator+=()

string

&append(const

string

&s,int

,int

n);//把字符串s中从开始的n个字符连接到当前字符串的结尾

string

&append(int

n,char

c);

//在当前字符串结尾添加n个字符c

string

&append(const_iterator

first,const_iterator

last);//把迭代器first和last之间的部分连接到当前字符串的结尾

string的比较:

bool

operator==(const

string

&s1,const

string

&s2)const;//比较两个字符串是否相等

运算符">","<",">=","<=","!="均被重载用于字符串的比较;

int

compare(const

string

&s)

const;//比较当前字符串和s的大小

int

compare(int

,

int

n,const

string

&s)const;//比较当前字符串从开始的n个字符组成的字符串与s的大小

int

compare(int

,

int

n,const

string

&s,int

2,int

n2)const;//比较当前字符串从开始的n个字符组成的字符串与s中

//2开始的n2个字符组成的字符串的大小

int

compare(const

char

s)

const;

int

compare(int

,

int

n,const

char

s)

const;

int

compare(int

,

int

n,const

char

s,

int

2)

const;

compare函数在>时返回1,<时返回-1,==时返回0

string的子串:

string

substr(int

=0,int

n=

n)

const;//返回开始的n个字符组成的字符串

string的交换:

void

swap(string

&s2);

//交换当前字符串与s2的值

string类的查找函数:

int

find(char

c,

int

=0)

const;//从开始查找字符c在当前字符串的位置

int

find(const

char

s,

int

=0)

const;//从开始查找字符串s在当前串中的位置

int

find(const

char

s,

int

,

int

n)

const;//从开始查找字符串s中前n个字符在当前串中的位置

int

find(const

string

&s,

int

=0)

const;//从开始查找字符串s在当前串中的位置

//查找成功时返回所在位置,失败返回string::n的值

int

rfind(char

c,

int

=n)

const;//从开始从后向前查找字符c在当前串中的位置

int

rfind(const

char

s,

int

=n)

const;

int

rfind(const

char

s,

int

,

int

n=

n)

const;

int

rfind(const

string

&s,int

=n)

const;

//从开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::n的值

int

find_first_of(char

c,

int

=0)

const;//从开始查找字符c次出现的位置

int

find_first_of(const

char

s,

int

=0)

const;

int

find_first_of(const

char

s,

int

,

int

n)

const;

int

find_first_of(const

string

&s,int

=0)

const;

//从开始查找当前串中个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::n

int

find_first_not_of(char

c,

int

=0)

const;

int

find_first_not_of(const

char

s,

int

=0)

const;

int

find_first_not_of(const

char

s,

int

,int

n)

const;

int

find_first_not_of(const

string

&s,int

=0)

const;

//从当前串中查找个不在串s中的字符出现的位置,失败返回string::n

int

find_last_of(char

c,

int

=n)

const;

int

find_last_of(const

char

s,

int

=n)

const;

int

find_last_of(const

char

s,

int

,

int

n=

n)

const;

int

find_last_of(const

string

&s,int

=n)

const;

int

find_last_not_of(char

c,

int

=n)

const;

int

find_last_not_of(const

char

s,

int

=n)

const;

int

find_last_not_of(const

char

s,

int

,

int

n)

const;

int

find_last_not_of(const

string

&s,int

=n)

const;

//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找

c语言的void append( void)具体是什么意思?

在C语言中,任何时候你都可以用其它类型的指针来代替void指针(在C++中同样可以),或者用void指针来代替其它类型的指针(在C++中需要进行强制转换),并且不需要进行强制转换。例如,你可以把char 类型的指针传递给需要void指针的函数。

什么时候使用void指针?

当进行纯粹的内存作时,或者传递一个指向未定类型的指针时,可以使用void指针。void指针也常常用作函数指针。

有些C代码只进行纯粹的内存作。在较早版本的C中,这一点是通过字符指针(char )实现的,但是这容易产生混淆,因为人们不容易判断一个字符指针究竟是指向一个字符串,还是指向一个字符数组,或者仅仅是指向内存中的某个地址。

C语言中string指令是什么?

C语言是一门面向过程的语言,它压根就没有string类类型,字符串在C语言中是用char s,或者char s[]来表示的,只有C++、C#等等面向结构的高级语言有string类类型,string s = “”;我不知道你说的string指令是什么意思,就算在C语言中你要封装成string的接口,也是不行的,因为string是关键字,无法做变量名和函数名来命名的。

如果还有问题,可以追问。

string在C语言中没有什么特别的含义,既不是关键字,也不是库函数。

string可能是编程人员自定义的一个变量名或函数名等。如:

char string; // 定义一个字符变量string

char string="abcd"; // 定义一个字符指针变量string

void string() // 定义一个函数,函数名为string

{printf("abc");

}

指的是字符串类

string s1;string s2="hello"

String类是不可变(final)的,对String类的任何改变,都是返回一个新的String类对象.这样的话把String类的引用传递给一个方法,该方法对String的任何改变,对原引用指向的对象没有任何影响,这一点和基本数据类型相似.

你是说头文件中的

还是说string类型呢?

楼上的回答不知道是否正确,C语言定义字符串是char 类型,或者char str[],用数组。

你说的string好像是C++的吧?

c语言中有类的概念吗?string类?没听过、、、

我仔细看了他,连对象都出来了,肯定不是C语言的了。。

你要说那我猜就是各种函数了吧,什么strcmp,strcpy,strlen等等。。。。

你把问题说明白点吧。。。

string:编程语言中的字符串

c语言里没有String类型

r,w,a+,r+在C语言中各自表示什么意思?

对于文件使用方式有以下几点说明:

1)文件使用方式由r,w,a,t,b,+六个字符拼成,各字符的含义是:

r(read):读

w(write):写

+:读和写

a(append):追加

t(text):文本文件,可省略不写

b(banary):二进制文件

2)用“r”打开一个文件时,该文件必须已经存在,且只能从该文件读出.

3)用“w”打开的文件只能向该文件写入.若打开的文件不存在,则以指定的文件名建立该文件,若打开的文件已经存在,则将该文件删去,重建一个新文件.

4)若要向一个已存在的文件追加新的信息,只能用“a”方式打开文件.但此时该文件必须是存在的,否则将会出错.

5)在打开一个文件时,如果出错,fopen将返回一个空指针值NULL.在程序中可以用这一信息来判别是否完成打开文件的工作,并作相应的处理.

C语言字符串的应用

你定义数组char

s[15]的时候系统已经自动在数组面加上0了.

表示按回车结束输入

if

(!strcmp(word,

"abcd"))

字符串比较不能使用==,否则比较的是指针。

另外字符串要用双引号。

C语言的简单字符串终于学习完了,功能是打印输出字符串