// OptFile.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;
//!计算字符串的长度
int myStrlen(const char pStr[])
{
for(int i=0;;i++)
{
if(pStr[i]=='\0')
{
return i;
}
}
}
//!计算字符串的长度
int myStrlenx(const char* pStr)
{
const char* pFirst = pStr;
while(*pFirst++ != '\0');
return pFirst - pStr -1;
}
//!执行函数
int _tmain(int argc, _TCHAR* argv[])
{
char szName[16] = "test123";
char szName2[16] = {'\0'};
strcpy(szName2,szName);
cout<<szName2<<endl;
//! 如果两个字符串相等,返回0,如果第一个大于第二个返回正数,小于,返回负数
if(strcmp("abd","abc") > 0)
{
cout<<"大于"<<endl;
}
const char* pStrA = NULL;
pStrA = strstr("abcabcabcabcdabc","abcde");
if(pStrA == NULL)
{
cout<<"没有找到"<<endl;
}else
{
cout<<pStrA<<endl;
}
cout<<myStrlenx(szName2)<<endl;
return 0;
rename("c:/a/abcabc.txt","c:/a/abc.txt");
return 0;
remove("c:/sss.txt");
FILE* pFile = fopen("c:/test.txt","r");
char szBuf[1024];
fgets(szBuf,1024,pFile);
cout<<szBuf<<endl;
fclose(pFile);
pFile = fopen("c:/test.txt","a+");
//! 向文件中写字符串函数
char* pStr = "你好,Game College";
fputs(pStr,pFile);
int iAge = 20;
char chChar = 's';
fprintf(pFile,
"%s-----%d --%c","网页",iAge,chChar);
fclose(pFile);
return 0;
C/C++读写文件实例
最新推荐文章于 2021-05-18 23:28:14 发布
本文介绍了一个使用C++实现的字符串长度计算方法及文件读写操作的示例。其中包括自定义字符串长度函数的实现,文件重命名、删除、打开、读取和写入的具体步骤。
11万+

被折叠的 条评论
为什么被折叠?



