C/C++读写文件实例

本文介绍了一个使用C++实现的字符串长度计算方法及文件读写操作的示例。其中包括自定义字符串长度函数的实现,文件重命名、删除、打开、读取和写入的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 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;

 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值