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"试试。
Although it is natural to assume that szStr1 and szStr2 contain the same values, the values they actually contain are shown in Figure 1.1.
Figure 1.1 Escapes and String Concatenation
Microsoft Specific
The maximum length of a string literal is approximately 2,048 bytes. This limit applies to strings of type char[] and wchar_t[]. If a string literal consists of parts enclosed in double quotation marks, the preprocessor concatenates the parts into a single string, and for each line concatenated, it adds an extra byte to the total number of bytes.
For example, suppose a string consists of 40 lines with 50 characters per line (2,000 characters), and one line with 7 characters, and each line is surrounded by double quotation marks. This adds up to 2,007 bytes plus one byte for the terminating null character, for a total of 2,008 bytes. On concatenation, an extra character is added to the total number of bytes for each of the first 40 lines. This makes a total of 2,048 bytes. (The extra characters are not actually written to the string.) Note, however, that if line continuations (\) are used instead of double quotation marks, the preprocessor does not add an extra character for each line.
END Microsoft Specific
Determine the size of string objects by counting the number of characters and adding 1 for the terminating '\0' or 2 for type wchar_t.
Because the double quotation mark (") encloses strings, use the escape sequence (") to represent enclosed double quotation marks. The single quotation mark (') can be represented without an escape sequence. The backslash character (\) is a line-continuation character when placed at the end of a line. If you want a backslash character to appear within a string, you must type two backslashes (\\). (SeePhases of Translation in the Preprocessor Reference for more information about line continuation.)
To specify a string of type wide-character (wchar_t[]), precede the opening double quotation mark with the character L. For example:
wchar_t wszStr[] = L"1a1g";
All normal escape codes listed in Character Constants are valid in string constants. For example:
cout << "First line\nSecond line";
cout << "Error! Take corrective action\a";
Because the escape code terminates at the first character that is not a hexadecimal digit, specification of string constants with embedded hexadecimal escape codes can cause unexpected results. The following example is intended to create a string literal containing ASCII 5, followed by the characters five:
\x05five"
The actual result is a hexadecimal 5F, which is the ASCII code for an underscore, followed by the characters ive. The following example produces the desired results:
"\005five" // Use octal constant.
"\x05" "five" // Use string splicing.
[解决办法]
[code=c][/code]
FILE *fp;
if((fp=fopen("C:\Users\Administrator\Desktop\1.txt","w"))==NULL) //以写文本方式打开文件
{\\注意转义字符\,正确的写法是\\,才是\,另外,这个路径,好像不对,可以写在当前目录,即不写路径,直接写文件名。
printf("open file failure!\n");
}
fputc('a',fp);
//如果写完了,记得要用fclose关闭文件。