Đăng Nhập

Vui lòng khai báo chính xác tên truy cập và mật khẩu!

Quên mật khẩu?

    CÁC THAO TÁC TRÊN FILE HỆ THỐNG XÓA FILE TIM FILE.. BẰNG C/C++

      Admin
      Admin

      Giới tính : Nam

      Đến từ : TPHCM

      Ngày Tham gia : 03/04/2011

      Tổng số bài gửi : 2292

      #1

       Fri Aug 12, 2011 10:05 am

      FILE VÀ HỆ THỐNG
      1. Xóa 1 file dùng Remove
      Code:

      #include <stdio.h>

      int main()
      {
      remove("d:/urls1.dat");

      return 0;
      }
      2. Xóa 1 File dùng Unlink
      Code:

      #include <stdio.h>

      int main()
      {
      remove("C:/pete.txt");

      return 0;
      }
      3. Cho biết thông tin FAT
      Code:

      #include <stdio.h>
      #include <dos.h>

      void main(void)
      {
      struct fatinfo fat;

      getfatd(&fat);

      printf("Sectors per cluster %d\n", fat.fi_sclus);
      printf("Clusters per disk %u\n", fat.fi_nclus);
      printf("Bytes per cluster %d\n", fat.fi_bysec);
      printf("Disk type %x\n", fat.fi_fatid & 0xFF);
      }
      4. Đếm tần suất 1 kí tự trong 1 file
      Code:

      # include <stdio.h>
      # include <string.h>
      main()
      {
       FILE *fp;
       char in[100];
       long int freq[257];
       int i;

       printf("\nFile frequency table generator\n\n");

       printf("\nInput file:");
       scanf("%s",in);
       fp=fopen(in,"rb");
       if(fp==NULL)
       {
        printf("\nCould not open input file.Aborting\n");
        return 1;
       }
       for(i=0;i<257;i++)
        freq[i]=0;
       while(i=fgetc(fp),i!=EOF)
       {
        freq[i]++;
       }
       fcloseall();
       fp=fopen("count.txt","w");
       fprintf(fp,"\nCharacter frequency table of %s\n",in);
       fprintf(fp,"\nCharacter ASCII frequency\n\n");
       for(i=0;i<256;i++)
       {
        if(i==26)
        {
        fprintf(fp,"\t    26\t  %ld\n",freq[26]);
        }
        else if(i==9)
        {
        fprintf(fp,"\t    9\t  %ld",freq[9]);
        }
        else if(i<10)
        {
        fprintf(fp,"%c\t    %d\t  %ld\n",i,i,freq[i]);
        }
        else if(i<100)
        {
        fprintf(fp,"%c\t    %d\t  %ld\n",i,i,freq[i]);
        }
        else
        {
        fprintf(fp,"%c\t    %d\t  %ld\n",i,i,freq[i]);
        }
       }
       
       fcloseall();
       printf("\nFrequency table copied to count.txt\n");
      }
      5. Đọc nội dung 1 file
      Code:

      #include <stdio.h>

      void main(void)
      {
      FILE *fp;
      char ch;

      fp = fopen("websites.txt","r");
      ch = getc(fp);
      while(ch!=EOF)
      {
      putchar(ch);
      ch = getc(fp);
      }
      printf("\n\n");
      }
      6. Chọn ổ đĩa trong DOS
      Code:

      #include <stdio.h>
      #include <dir.h>

      void main(void)
      {
      int drives;

      drives = setdisk(3);
      printf("The number of available drives is %d\n", drives);
      }
      7.Chọn ổ đĩa trong WINS
      Code:

      #include <windows.h>
      #include <stdio.h>
      #include <stdlib.h>

      void main(void)
       {
          char szBuffer[MAX_PATH+100];
          UINT nDrive, AvailDrive = 0;
          int dwLogicalDrives = GetLogicalDrives();
          DWORD Success;

          printf("Number of logical drives: %d\n", dwLogicalDrives);
             for (nDrive = 0; nDrive < 32; nDrive++)
            {
               if (dwLogicalDrives & (1 << nDrive))
                { // Is drive available?
                  AvailDrive++;
                  // Get disk information.
                  wsprintf(szBuffer, "%c:\\", nDrive+'A', '\0');
                  // Print out information.
                  if(SetCurrentDirectory(szBuffer))
                    printf("%s Is Now Current\n", szBuffer);
                 else
                    printf("Could not set %s as the current drive\n", szBuffer);
                }
            }
            printf("Number of drives available: %d\n", AvailDrive);

       }
      8. Cho biết kích thước 1 file
      Code:

      #include <stdio.h>
      #include <io.h>
      #include <fcntl.h>
      #include <sys\stat.h>

      int main()
      {
      int fp;

      long file_size;

      if ((fp = open("f:/cprojects/urls.txt", O_RDONLY)) == -1)
      printf("Error opening the file \n");
      else
      {
      file_size = filelength(file_handle);
      printf("The file size in bytes is %ld\n", file_size);
      close(fp);
      }
      return 0;
      }