首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

菜鳥-從一個文件讀內容寫入另一個文件

2012-04-15 
菜鳥求助-----從一個文件讀內容寫入另一個文件菜鳥請大俠們幫忙要從1.txt中讀取內容寫到st.txt中。1.txt有

菜鳥求助-----從一個文件讀內容寫入另一個文件
菜鳥請大俠們幫忙要從1.txt中讀取內容寫到st.txt中。1.txt有多少行是不確定的,內容如下:
;for Title of report|File name of data
;--------------------------------------------------
PART NUM|c:\record\pt.dat
SFIS NUM|c:\record\sfis.dat
STATUS|c:\record\status.dat
ERROR CODE|c:\record\error.dat
;--------------------------------------------------
第一步:讀取時候要略過有";"分號的行,抓取"|"前面的字符寫入2.txt,每個值用"|"隔開,2.txt的最前面是"# ",最後不要加"|"。內容是這個樣子的。
# PART NUM|SFIS NUM|STATUS|ERROR CODE
第二步:讀取1.txt中"|"後面的內容,是個文件路徑,讀取此文件容,追加到剛才的2.txt中。如下:
# PART NUM|SFIS NUM|STATUS|ERROR CODE
11231|123|4564|456|132231|12223

請高手幫忙寫段代碼。感謝。。。。




[解决办法]
以下代码已验证:

C/C++ code
#include <afx.h>void main(){    CStdioFile mFile;    CString strTemp;    mFile.Open( "1.txt ",CFile::modeRead);    int location;    CString strResult1 = _T("# ");    CString strResult2 = _T("");    if(mFile.m_pStream != 0x00000000)  //文件不为空    {        while (mFile.ReadString(strTemp))   //逐行读取        {            if(strTemp != _T(""))            {                location = strTemp.Find(_T(";"));  //获取读到的内容中';'的位置                                if (location == -1)  //没有';'的时候再进行操作                {                                        location = strTemp.Find(_T("|"));  //获取读到的内容中'|'的位置                    if (location != -1)  // 有'|'的时候再进行操作                    {                        strResult1 += strTemp.Mid(0, location) + _T("|");                        strResult2 += strTemp.Mid(location + 1, strTemp.GetLength() - location - 1) + _T("|");                    }                }            }        }    }    strResult1 = strResult1.Mid(0, strResult1.GetLength() - 1);    strResult2 = strResult2.Mid(0, strResult2.GetLength() - 1);    mFile.Open( "2.txt ",CFile::modeCreate | CFile::modeWrite);    mFile.SeekToEnd();   //光标移到最后    mFile.WriteString(strResult1 + _T("\n") + strResult2);}
[解决办法]
楼主看下这个行不行:
C/C++ code
#include<stdio.h>#include<stdlib.h>#include<string.h>void main(){    FILE *fp1,*fp2;    char str[50][100];    char st[200];    char stt[200];    int i,j,k,t,ii,jj;    fp1 = fopen("1.txt", "r");    if(fp1 == NULL)    {        exit(1);    }    k = 0;    while(fgets(str[k++], 80, fp1) != NULL);    --k;    fclose(fp1);    memset(st, 0, sizeof(st));    memset(stt, 0, sizeof(stt));            st[0] = '#';    st[1] = ' ';    ii = 2;jj = 0;    for(i = 0; i < k; ++i)    {        if(str[i][0] == ';')        {            continue;        }        t = 0;        for(j = 0;j < strlen(str[i]); ++j)        {            if(str[i][j] == '|')            {                st[ii++] = str[i][j];                t = 1;                continue;            }            if(t==0 && str[i][j] != '\n')            {                st[ii++] = str[i][j];            }            else if(str[i][j] != '\n')            {                stt[jj++] = str[i][j];            }                    }        stt[jj++] = '|';                    }    fp2 = fopen("2.txt", "w");    if(fp2 == NULL)    {        exit(1);    }    fputs(st, fp2);    fputs(stt, fp2);    fclose(fp2);} 

热点排行