c语言关于文件打开与写字符的有关问题
c语言关于文件打开与写字符的问题
FILE *fp;
if((fp=fopen("C:\Users\Administrator\Desktop\1.txt","w"))==NULL) //以写文本方式打开文件
{
printf("open file failure!\n");
}
fputc('a',fp);
这个程序怎么提示open file failure呢?这程序快编完了 结果这个问题纠结了一早上没解决,还得求助大神们
还有有时编程打开后往文件里写字符,怎么写完后打开一看里面什么都没有呢
------解决方案--------------------
改为"C:\\Users\\Administrator\\Desktop\\1.txt"试试。
------解决方案--------------------
C++ String Literals
A string literal consists of zero or more characters from the source character set surrounded by double quotation marks ("). A string literal represents a sequence of characters that, taken together, form a null-terminated string.
Syntax
string-literal :
"s-char-sequenceopt"
L"s-char-sequenceopt"
s-char-sequence :
s-char
s-char-sequence s-char
s-char :
any member of the source character set except the double quotation mark ("), backslash (\), or newline character
escape-sequence
C++ strings have these types:
Array of char[n], where n is the length of the string (in characters) plus 1 for the terminating '\0' that marks the end of the string
Array of wchar_t, for wide-character strings
The result of modifying a string constant is undefined. For example:
char *szStr = "1234";
szStr[2] = 'A'; // Results undefined
Microsoft Specific
In some cases, identical string literals can be “pooled” to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. The/Gf compiler option enables string pooling.
END Microsoft Specific
When specifying string literals, adjacent strings are concatenated. Therefore, this declaration:
char szStr[] = "12" "34";
is identical to this declaration:
char szStr[] = "1234";
This concatenation of adjacent strings makes it easy to specify long strings across multiple lines:
cout << "Four score and seven years "
"ago, our forefathers brought forth "
"upon this continent a new nation.";
In the preceding example, the entire string Four score and seven years ago, our forefathers brought forth upon this continent a new nation. is spliced together. This string can also be specified using line splicing as follows:
cout << "Four score and seven years \
ago, our forefathers brought forth \
upon this continent a new nation.";
After all adjacent strings in the constant have been concatenated, the NULL character, '\0', is appended to provide an end-of-string marker for C string-handling functions.
When the first string contains an escape character, string concatenation can yield surprising results. Consider the following two declarations:
char szStr1[] = "\01" "23";
char szStr2[] = "\0123";
FILE *fp;
if((fp=fopen("C:\Users\Administrator\Desktop\1.txt","w"))==NULL) //以写文本方式打开文件
{
printf("open file failure!\n");
}
fputc('a',fp);
这个程序怎么提示open file failure呢?这程序快编完了 结果这个问题纠结了一早上没解决,还得求助大神们
还有有时编程打开后往文件里写字符,怎么写完后打开一看里面什么都没有呢
------解决方案--------------------
改为"C:\\Users\\Administrator\\Desktop\\1.txt"试试。
------解决方案--------------------
C++ String Literals
A string literal consists of zero or more characters from the source character set surrounded by double quotation marks ("). A string literal represents a sequence of characters that, taken together, form a null-terminated string.
Syntax
string-literal :
"s-char-sequenceopt"
L"s-char-sequenceopt"
s-char-sequence :
s-char
s-char-sequence s-char
s-char :
any member of the source character set except the double quotation mark ("), backslash (\), or newline character
escape-sequence
C++ strings have these types:
Array of char[n], where n is the length of the string (in characters) plus 1 for the terminating '\0' that marks the end of the string
Array of wchar_t, for wide-character strings
The result of modifying a string constant is undefined. For example:
char *szStr = "1234";
szStr[2] = 'A'; // Results undefined
Microsoft Specific
In some cases, identical string literals can be “pooled” to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. The/Gf compiler option enables string pooling.
END Microsoft Specific
When specifying string literals, adjacent strings are concatenated. Therefore, this declaration:
char szStr[] = "12" "34";
is identical to this declaration:
char szStr[] = "1234";
This concatenation of adjacent strings makes it easy to specify long strings across multiple lines:
cout << "Four score and seven years "
"ago, our forefathers brought forth "
"upon this continent a new nation.";
In the preceding example, the entire string Four score and seven years ago, our forefathers brought forth upon this continent a new nation. is spliced together. This string can also be specified using line splicing as follows:
cout << "Four score and seven years \
ago, our forefathers brought forth \
upon this continent a new nation.";
After all adjacent strings in the constant have been concatenated, the NULL character, '\0', is appended to provide an end-of-string marker for C string-handling functions.
When the first string contains an escape character, string concatenation can yield surprising results. Consider the following two declarations:
char szStr1[] = "\01" "23";
char szStr2[] = "\0123";