在c中使用`putw`和`getw`函数有什么用?

我想知道putw()getw()函数的使用。 据我所知,这些用于从文件中读取和读取就像putcgetc一样,但这些只处理整数。但是当我使用它们来编写整数时,它只是在文件中写入不同的符号(就像我写入65文件一样)使用putw() 。它在文件中写入A )。 为什么采用ASCII值? 我正在使用代码块13.12。 码:

 #include int main() { FILE *fp; int num; fp=fopen("file.txt","w"); printf("Enter any number:\n"); scanf("%d",&num); putw(num,fp); fclose(fp); printf("%d\n",num); return 0; } 

 lets read the point to point explanation of getw() and putw() function in c programming language. getw() and putw() functions in c programming language, getw() and putw() functions are related to FILE handling in c programming language. putw() function in c programming language is use to write integer data on the file (text file). getw() function in c programming language is use to read the integer data from the file. getw() and putw() functions are same as the getc() and putc() functions in c programming language but the only difference is that getw() and putw() function the especially meant for reading and writing the integer data on the file. putw() Syntax of putw() function in c programming langugae as shown below:- int putw(integer, FILE*); Return type of the function in integer. Having two argument first "integer", telling the integer you want to write on the file and second argument "FILE*" telling the location of the file in which the data would be get written. Now lets see an example to understand the concept of putw() in c programming language. example :- void main() { FILE *fp; fp=fopen("file1.txt","w"); putw(65,fp); fclose(fp); } Here putw() function take the integer number as argument (65 in this case) to write it on the file file1.txt, but if we manually open the text file we find 'A' written on the file. It means that putw() function actually take integer argument but write it as character on the file. So, it means that compiler take the argument of the putw() function as the ASCII code of the particular character and write the character on the text file. 

getw()

  Syntax of getw() function in c programming language is as shown below:- int getw(FILE*); Return type is integer. Having one argument that is FILE* that is the location of the file from which the integer data to be read. 

现在让我们看一下这些例子,以便更好地理解c编程语言中getw()函数的概念。

例如: –

在下面的示例中,我们将读取我们在上面示例中名为file1.txt的文件中编写的数据。 void main(){FILE * fp; int ch;

  fp=fopen("file1.txt","r"); ch=getw(fp); printf("%d",ch); fclose(fp); } output 65 

说明: – 这里我们读取我们在上面程序中的file1.txt上写的数据,我们将输出为65.所以,getw()函数读取已经写在文件file1.txt上的字符’A’并返回65的字符“A”的ASCII码。

 we can also write the above program as shown below:- void main() { FILE *fp; fp=fopen("file1.txt","r"); printf("%d",getw(fp)); fclose(fp); } 

输出65

putw()getw()的手册页

getw()从流中读取一个单词(即int)。 它提供与SVr4的兼容性。

putw()将单词w(即int)写入流。 它是为了与SVr4兼容而提供的。

您可以使用fread和fwrite函数以便更好地使用。

getw()从给定的FILE流中读取整数。

putw()它将第一个参数中给出的整数写入文件指针。

getw:

它将从文件中读取整数。 像getchar()做的工作。 考虑具有内容“hello”的文件。 它将读取h并返回h的ascii值。

putw:

它将给定的整数,整数作为ascii值。 放置在文件中的ascii值的对应值。 喜欢putchar()

由于它是ASCII文本文件,因此文件视图将始终为ASCII,尽管整数写入文件。