c++ 文件读写

// Test.cpp : Defines the entry point for the console application.
//vc6.0 编译通过
//2023-10-04

#include <iostream>
#include <string>
#include <shlobj.h>

#define STRICT_TYPED_ITEMIDS

using namespace std;

string getFileName()
{    
    OPENFILENAME ofn;    
    char file_name[100];
    
    ZeroMemory(&ofn, sizeof(OPENFILENAME));
    
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = NULL;
    ofn.lpstrFile = file_name;
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = 100;
    ofn.lpstrFilter = TEXT("All\0*.*\0Excel\0*.xls\0") ;
    ofn.nFilterIndex = 1;    
    GetOpenFileName(&ofn);

    return (const char*)ofn.lpstrFile  ;
}

int main()
{
    cout << getFileName() << endl;
    
    return 0;
}

/

#include <stdio.h>
#include <string.h>  

//结构声明 
struct Item
{
   char name[24];
   float  val;
};

void Writefile(Item & a)
{
    strcat (a.name,"\0");
    char ch;
    FILE *fp;//定义文件流指针,用于打开文件   
    fp = fopen("0063120.txt","a");//写打开文件   
    fprintf(fp ,"%s\t%0.2f\n",  a.name, a.val);
    fclose(fp);//关闭文件a,有打开就要有关闭   
}


void Readfile()
{
     struct Item b;
    FILE *fp;//定义文件流指针,用于打开文件   
    fp = fopen("0063120.txt","r");//写打开文件  

    while(!feof(fp))
    {        
        fscanf(fp, "%s\t%f", b.name, &b.val);
        
        if(strlen(b.name) == 0) break;
        
        printf("%s\t%0.2f\n", b.name, b.val);
            
        memset(b.name, 0,sizeof(b.name)) ;    
    }

    fclose(fp);
}

int main()
{
    //struct Item a={ "中梃", 2400};   //结构定义并赋值 
    
    //Writefile(a);
    Readfile();
    
    getchar();
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值