求高手指点 主要是WriteString和ReadString的问题
我有两段代码 一段负责向文件中写字符串 另一段负责从文件中读出字符串 读写都需要按行进行,读出的字符串要显示在组合框的下拉列表中;
写文件的代码如下:CStdioFile imgFile;
imgFile.Open(FILEPATH, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeRead | CFile::modeWrite); //FILEPATH为文件绝对路径
imgFile.SeekToEnd();
imgFile.WriteString(m_FileName);
imgFile.Close();读文件的代码如下:CStdioFile imgFile;
CComboBox *pCmbComImg = (CComboBox*)GetDlgItem(IDC_COMBO_IMG); //对话框中的组合框控件
pCmbComImg->ResetContent();
CString strImgName = L"";
if(imgFile.Open(FILEPATH, CFile::modeRead))
{
while(imgFile.ReadString(strImgName))
{
pCmbComImg->AddString(strImgName);
strImgName = L"";
}
}
else //创建文件
{
if(!imgFile.Open(FILEPATH, CFile::modeCreate | CFile::modeRead))
{
AfxMessageBox(_T("创建数据库失败"));
return FALSE;
}
}
imgFile.Close();
pCmbComImg->SetCurSel(0);
我单步Debug的时候发现ReadString函数执行的时候一次直接读取多行到CString对象中了?这是怎么回事?
当把CString对象添加到组合框的时候所有字符串都显示在一行了,并且字符串之间有空格?

解决方案 »

  1.   

    你写的时候有没有一行一行的写进去,每写一行,要再写一行换行符imgFile.WriteString(_T("aaa"));
    imgFile.WriteString(_T("\r\n"));
      

  2.   

    如果文件中有三行数据
    比如
    test01
    test02
    test03
    单步Debug的时候执行到while(imgFile.ReadString(strImgName))
    这句的时候 strImgName的值是“test01test02test03” 是一次读到的 不是分行读出来的
      

  3.   

    怎么发给你?邮箱?
    我这个程序是跑在winCE下的
      

  4.   

    有2种:
    //CFile::modeRead|CFile::typeText[/color|CFile::shareDenyNone;
    //CFile::modeRead|CFile::[color=#FF0000]typeBinary
    |CFile::shareDenyNone;   
    //二进制时要加'\r\n'
    //文本时只加'\n'
      

  5.   

    这个我在网上查到了 不过应该不是这个问题
    WINCE下貌似不支持CFile::typeText, 代码里加上这个标志之后会报错
    而且CStdioFile默认的就是文本模式的吧
      

  6.   

    CStdioFile imgFile;
    imgFile.Open(FILEPATH, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeRead | CFile::modeWrite); //FILEPATH为文件绝对路径
    imgFile.SeekToEnd();
    imgFile.WriteString(m_FileName);
    imgFile.Close();
      

  7.   

    try
    CStdioFile.WriteString("....\n")
    代替
    CStdioFile.WriteString("....\r\n")
      

  8.   

    //直接用C库
       FILE *stream;
       char line[100];   if( (stream = fopen( "xxxx.c", "rb" )) != NULL )
       {
          if( fgets( line, 100, stream ) == NULL)
             printf( "fgets error\n" );
          else
             printf( "%s", line);
          fclose( stream );
       }
      

  9.   

    代码里已经写上了 但是还是老问题 
    是不是ReadString函数的问题呀
      

  10.   

    代码里已经写上了 但是还是老问题 
    是不是ReadString函数的问题呀
      

  11.   

    WINCE下可以用这样的方式读文件的一行:FILE * f = NULL;
    f = fopen(cFileName,"r");
    if ( f == NULL )
    {
       return ;
    }
    char szBufLine[1024];while (true)
    {
       memset(szBufLine, 0, sizeof(szBufLine));
       char *pRet = NULL;
       pRet = fgets(szBufLine, 1022, f);
       string strBuf = szBufLine;
       int last = strBuf.find_last_of("\n");
       if ( last == -1 )
       {
             string strTemp = strBuf.substri(0,last);
            strcpy(szBufLine,strTemp.c_str());
        }
        if ( pRet == NULL )
        {
              break;
          } 
    }
    fclose(f);
      

  12.   

    imgFile.WriteString(_T("aaa\n"));
    我反正是这样用的  没问题
      

  13.   

    用uedit打开看看(Hex)写的对不对
      

  14.   

    char cFileName[1024] = "\Windows\1.txt";//文件的路径FILE * f = NULL;
    f = fopen(cFileName,"r");//打开文件
    if ( f == NULL )
    {
       return ;//打开失败,返回
    }
    char szBufLine[1024];//一行数据的缓冲区
    //下面循环读取文件中的各行
    while (true)
    {
       memset(szBufLine, 0, sizeof(szBufLine));//要先清空,以免有乱码哦。
       char *pRet = NULL;
       pRet = fgets(szBufLine, 1022, f);//从文件中读取1022个字符到szBufLine,但是中途遇到 换行,也就是\n,则停下,
       //szBufLine包含一行数据,\n也包含在内
       if ( pRet == NULL )
       {
       break;//读取出错,可能是到文件尾了,退出循环
       } 
       string strBuf = szBufLine;//赋值语句,用string的方法来截取一行数据的后面\n
       int last = strBuf.find_last_of("\n");//打到字符串的\n
       if ( last != -1 )
       {
       //last != -1 表示找到\n
           string strTemp = strBuf.substri(0,last);//把\n前面的字符串截取到,strTemp里保存的就是一行的数据了,呵呵
           strcpy(szBufLine,strTemp.c_str());//把一行数据从string里复制到szBufLine
        }
    }
    fclose(f);//关闭打开的文件考虑你初学,应你要求加上注释,希望还能对其它朋友也有帮助,开始是直接打进去的,有些地方写错,代码调整下了。