| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1152 人关注过本帖
标题:关于用VC写控制台程序的几个小疑问?
取消只看楼主 加入收藏
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
结帖率:97.66%
收藏
已结贴  问题点数:20 回复次数:10 
关于用VC写控制台程序的几个小疑问?
求高手~VC写控制台程序~
1.能用API吗?有跟MFC一样内建吗?
2.搜寻特定资料夹下的特定相似档案要怎写?
3.档案全路径怎切割为档案路径和档名?
4.这用VB写超简单~纯C我就头大了~又没CString的这个类能用~全都要用指针和双指针去实现~好烦~

P.S 档案是由参数带入的~所以事先无法得知~

int main(int argc, char* argv[])
{
    ......
    //argc=3;argv[0]="AAA.exe";argv[1]="3";argv[2]="C:\\111\\222\\333_4444_555_6666666_77_88_120215180213.txt"
    //0:执行档名称 1:选择参数 2:档案路径
}

这只是一小部分~后面还一大串~
搜索更多相关主题的帖子: 切割 控制台 资料 
2012-02-16 19:46
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
那有不用CString的的做法吗?

不要選我當版主
2012-02-16 20:08
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
比这种简单的纯C写法?
程序代码:
//------------------------------------------------------------------------------------------------------------------//
int main(int argc, char* argv[])
{
    int i=0, j=0, k=0, len=0, opt=0;
    char *ex, *config=0;
    char FN[256];
    char PathArr[256];
    char FilePath[256]={0};
    char wFilePath[256]={0};
    char eFilePath[256]={0};
    char FileName[1024]={0};
    
  //time_t ltime;
    char cpDate[25]={0};
    char cpTime[25]={0};
    char cpFilename[50]={0};
   //time( &ltime );
    
    argc=3;argv[1]="3";argv[2]="C:\\STDF\\33\\AAA_BBBBBB_CCC_DDDDD_00_02_120215180213.std";

    opt=atoi(argv[1]);        //因為0:執行檔名稱 1:選擇參數 2:檔案路徑

    _strtime( cpTime );
    _strdate( cpDate );
    
    strncpy(cpFilename, cpDate+6,2);
    strncpy(cpFilename+2, cpDate,2);
    strncpy(cpFilename+4, cpDate+3,2);
    strncpy(cpFilename+6, cpTime,2);
    strncpy(cpFilename+8, cpTime+3,2);
    strncpy(cpFilename+10, cpTime+6,2);
    time_t unixtime=0;
    time(&unixtime);

    strcpy(PathArr, argv[2]);
    printf("\nThe Path is %d: %s\n\n",opt,PathArr);
    printf("1:STDF Transform ATDF.\n2:ATDF Transform STDF(No Finish).\n3:STDF Merge STDF.\n4.ATDF Merge ATDF(No Finish).\n");
    strcpy(FN,PathArr);
    ex=strrchr(PathArr,'.');

    int nIndex =strlen(PathArr) -sizeof ("100603194915.std");
      
      PathArr[nIndex +1] ='*';
      PathArr[nIndex +2] ='.';
      PathArr[nIndex +3] ='s';
      PathArr[nIndex +4] ='t';
      PathArr[nIndex +5] ='d';
      PathArr[nIndex +6] ='\0';

      for(i=0;i<sizeof(PathArr);i++)
      {
           if (PathArr[i]=='\\')
            j=i;
      }
      for(i=sizeof(PathArr);i>=0;i--)
      {
           if (PathArr[i]=='\0')
            k=i;
      }
      
      memcpy( FilePath, PathArr, j+1 );
      strncpy( FileName,PathArr+j+1,k-j );
      nIndex =strlen(FileName);
      FileName[nIndex -5] ='\0';
      strcat(FileName,cpFilename);
      memcpy(wFilePath, FilePath, strlen(FilePath));
      nIndex =strlen(wFilePath);
      wFilePath[nIndex ] ='T';
      wFilePath[nIndex +1] ='r';
      wFilePath[nIndex +2] ='a';
      wFilePath[nIndex +3] ='n';
      wFilePath[nIndex +4] ='s';
      wFilePath[nIndex +5] ='f';
      wFilePath[nIndex +6] ='o';
      wFilePath[nIndex +7] ='r';
      wFilePath[nIndex +8] ='m';
      wFilePath[nIndex +9] ='\\';
      
      if( (_access(wFilePath, 0 )) == -1 ) 
        int intRet = (_mkdir(wFilePath));

      strcat(wFilePath,FileName);
      strcpy(eFilePath, wFilePath);
      nIndex =strlen(wFilePath);
      wFilePath[nIndex ] ='.';
      wFilePath[nIndex +1] ='a';
      wFilePath[nIndex +2] ='t';
      wFilePath[nIndex +3] ='d';
      wFilePath[nIndex +4] ='f';
      wFilePath[nIndex +5] ='\0';

      nIndex =strlen(eFilePath);
      eFilePath[nIndex ] ='.';
      eFilePath[nIndex +1] ='l';
      eFilePath[nIndex +2] ='o';
      eFilePath[nIndex +3] ='g';
      eFilePath[nIndex +4] ='\0';
      
      len=strlen(ex);

      if((opt==1)&&(len==4))
            ST2AT(1,FN,wFilePath,eFilePath);
      else if((opt==2)&&(len==5))
            AT2ST(2,FN,wFilePath,eFilePath);
      else if((opt==3)&&(len==4))
            ST2MG(3,FN,wFilePath,eFilePath);
      else if((opt==4)&&(len==5))
            AT2MG(4,FN,wFilePath,eFilePath);
      else
            printf("opt Number Input Error!");

      printf("Work Finish !!!");

    //system("pause");
    return 0;
}

不要選我當版主
2012-02-16 20:13
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
后面还有数千行要全贴吗?

不要選我當版主
2012-02-16 20:15
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

刚好不容易在Win 7 64位的操作系统下装了VS6.0~试了一下_splitpath能用的~
这样光撷取路径和档名就少了好多行代码了~
超感谢~

不过还一个问题~
找档案夹下的相似档案列表清单~批次处理~这要怎做?

我在VB都是用Microsoft Scripting RunTime + for each + instr 运行~但是在C就不清楚了~
有简单点的吗?谢谢~

不要選我當版主
2012-02-16 22:05
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
找档案夹下的相似档案列表清单~
有清单我会写批次处理~
重点是我不会用C找档案夹下的相似档案列表清单~

不要選我當版主
2012-02-16 22:30
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
_finddata_t 列出清单是這個嗎?

不要選我當版主
2012-02-16 23:16
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
http://topic.
找到了~明天去试试~
多谢了~

不要選我當版主
2012-02-16 23:18
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
举例来说~

类似C:\ AAA\11111_22222_01_123456.txt~
要找出C:\ AAA\底下所有11111_22222_01_??????.txt档案~
01有时是02或03...等~01可能有N个档~02可能有M个档~各自分批处理~

并依照??????排先后顺序~
再做批次处理处理内容又是另一道复杂程序~
源代码已经有了~但是现在要加功能~而且以前人写的太(基本)了~
是能用但是代码很长~还有BUG~想改短些~

[ 本帖最后由 wube 于 2012-2-17 13:34 编辑 ]

不要選我當版主
2012-02-17 13:30
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
有些东西跟VB好像~
程序代码:
Option Explicit

Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MaxLFNPath = 260
Const INVALID_HANDLE_VALUE = -1

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MaxLFNPath
    cShortFileName As String * 14
End Type

Dim WFD As WIN32_FIND_DATA
Dim bgndir$, curpath$, schpattern$, aa$, fname$, progdisk$
Dim hItem&, hFile&, rtn&, i%, j%, k%, tfiles&, tfsize#, stopyn As Boolean 'Boolean 数据类型 (Visual Basic)存放只可能为 True 或 False 的值
Dim X1&, buff$ 'Dim x1& 是Dim x1 As Long“长整型”& 是 As Long的缩写,! 是 as single 的缩写,例如:dim x0!,x1!,t!(或:dim x0 as single,x1 as single,t as single)

Private Sub cmdBower_Click()
Dim Path As String

    Path = BrowseForFolder(Me.hwnd, "Select Project's Location :", , NEWFOLDER)
    If (Trim(Path) <> "") Then
        txtTargetPath.Text = IIf(Right(Trim(Path), 1) <> "\", Path & "\", Path)
        txtTargetPath.ToolTipText = txtTargetPath.Text
        cmdSearch.Enabled = True
        SelectTargetPath = Path
    End If
    
End Sub

Private Sub CmdExit_Click()
    Call WriteKTPList
    Unload A_frmSearchKTP
    A_MainForm.Show
End Sub

Private Sub WriteKTPList()
Dim FileName As String, TempString As String
Dim i As Integer, j As Integer, FileNum As Integer
    
    FileName = App.Path & "\" & "CSV"
    If IsFolderExist(FileName) = False Then MkDir FileName
    FileName = FileName & "\KTPList.ini"
    FileNum = FreeFile
    If List1.List(0) <> "" Then
        j = 1
        Open FileName For Output As #FileNum
            For i = 0 To List1.ListCount
                If List1.List(i) <> "" Then
                    Print #FileNum, j & "=" & List1.List(i)
                    j = j + 1
                End If
            Next i
        Close #FileNum
    End If
End Sub

Private Sub cmdSearch_Click()
Dim s As String

On Error Resume Next
    
    List1.Clear '清空list1里面的内容
    tfiles = 0: tfsize = 0 '初始化统计文件数为0,文件大小为0,其中冒号是将两个语句分隔开
    stopyn = False 'stopyn估计是按钮的停止属性跟cancel差不多吧
    
    CmdExit.Enabled = Not CmdExit.Enabled
    cmdBower.Enabled = Not cmdBower.Enabled
    cmdSearch.Enabled = Not cmdSearch.Enabled
    cmdSTOP.Enabled = Not cmdSTOP.Enabled
    Text1.Locked = Not Text1.Locked
    Text2.Locked = Not Text2.Locked
    
    If InStr(Text1.Text, ".") = 0 Then Text1.Text = Trim(Text1.Text) & "*.*"    '在text1中查找"."如果"."是第一个则....

    s = Trim(txtTargetPath.Text)
    
    bgndir = s '开始搜的文件夹
    If InStr(bgndir, ":") = 0 And Len(bgndir) = 1 Then bgndir = bgndir & ":"
    If Right(bgndir, 1) <> "\" Then bgndir = bgndir & "\"
    schpattern = Trim(Text1.Text) '模糊搜索条件,例如 *.* 或 *.mp3 或 sc*.*
    Call SearchDirs(bgndir)
    
    If tfiles > 0 Then
        MsgBox "搜索完成,共查找到" & str(tfiles) & " 个文件" & vbCrLf & Chr(10) & "总占空间: " & Format(str(tfsize), "#,###") & " Bytes"
    Else
        MsgBox "搜索完成,未找到符合的文件"
    End If
    
    cmdBower.Enabled = Not cmdBower.Enabled
    cmdSearch.Enabled = Not cmdSearch.Enabled
    cmdSTOP.Enabled = Not cmdSTOP.Enabled
    CmdExit.Enabled = Not CmdExit.Enabled
    Text1.Locked = Not Text1.Locked
    Text2.Locked = Not Text2.Locked
    
    Me.Caption = "快速搜索文件"
    
End Sub

Private Sub cmdSTOP_Click()
    stopyn = True
End Sub

Private Sub Form_Load()
    Call init '窗体加载时,首先调用init配置过程
End Sub

Private Sub SearchDirs(curpath)
Dim dirs%, dircount%, dirbuf$()
Dim FilterName As String

On Error Resume Next
    
    Me.Caption = "正在查找 " & curpath
    DoEvents
    hItem = FindFirstFile(curpath & "*", WFD)
    
    If hItem <> INVALID_HANDLE_VALUE Then
        Do
            DoEvents
            If stopyn Then Exit Do
            FilterName = IIf(Trim(Text2.Text) <> "", Trim(Text2.Text), "")
            If FilterName <> "" Then
                If InStr(WFD.cFileName, FilterName) = 0 Then
                    If (WFD.dwFileAttributes And vbDirectory) And Asc(WFD.cFileName) <> 46 Then
                    If (dirs Mod 10) = 0 Then ReDim Preserve dirbuf(dirs + 10)
                    dirs = dirs + 1
                    dirbuf(dirs) = Left(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
                    End If
                End If
            Else
                If (WFD.dwFileAttributes And vbDirectory) And Asc(WFD.cFileName) <> 46 Then
                    If (dirs Mod 10) = 0 Then ReDim Preserve dirbuf(dirs + 10)
                    dirs = dirs + 1
                    dirbuf(dirs) = Left(WFD.cFileName, InStr(WFD.cFileName, vbNullChar) - 1)
                End If
            End If
        Loop While FindNextFile(hItem, WFD)
        
        Call FindClose(hItem)
        Call mohusearch(curpath)
    
    End If
    
    For dircount = 1 To dirs
        DoEvents
        If stopyn Then Exit For
        SearchDirs curpath & dirbuf$(dircount) & "\"
    Next dircount
    
End Sub

Private Sub mohusearch(curpath)

On Error Resume Next

    hFile = FindFirstFile(curpath & schpattern, WFD)
    
    If hFile <> INVALID_HANDLE_VALUE Then
    
        Do
            DoEvents
            If stopyn Then Exit Do
            aa = Trim(Trim(curpath) & Trim(WFD.cFileName))
            If (WFD.dwFileAttributes And vbDirectory) Or Asc(WFD.cFileName) = 46 Then
                
            Else
                k = InStr(aa, Chr(0))
                If k > 0 Then
                    fname = Mid(aa, 1, k - 1)
                    aa = fname '& " ----- " & Format(str(FileLen(fname)), "#,###") & " Bytes"
                    tfiles = tfiles + 1
                    tfsize = tfsize + FileLen(fname)
                    List1.AddItem aa
                    List1.Selected(List1.ListCount - 1) = True
                End If
            End If
        Loop While FindNextFile(hFile, WFD)
        
        Call FindClose(hFile)
        
    End If
    
End Sub

Private Sub List1_dblClick()
    If List1.ListCount > 0 Then
        j = List1.ListIndex
        fname = Trim(List1.List(j))
        j = InStr(fname, "-----")
        If j > 0 Then
            fname = Trim(Mid(fname, 1, j - 1))
            Shell "explorer " & fname, vbNormalNoFocus
        End If
    End If
End Sub

Private Sub init() '配置窗体加载
    Text1.Text = "*.KTP"
    'cmdSearch.Enabled = False
    cmdSTOP.Enabled = False
End Sub

不要選我當版主
2012-02-17 13:40
快速回复:关于用VC写控制台程序的几个小疑问?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031127 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved