防止字符在c中写入文件

时间:2017-09-05 16:25:10

标签: c pointers strncmp

我试图在c中编写一个程序来写出一些字符串,单词,字符等等...程序的要点是允许写入所有内容,但是程序的前两个字节不能包含字符" MZ"。我是c和指针的新手,这是我尝试过的:

方法1:这让我编译错误....

    else if (offset == 0L){                                                                                                                │  write_file_at(f, "MZ", 2, BEGINNING_OF_FILE, 0L);                                           
                                                                                                                                           │  fs_print_error();                                                                           
            if(!strncmp(((char*)data)[0], "M", 1)){                                                                                        │  printf("\n\n");                                                                             
                    if(offset == 1L && !strncmp(((char*)data), "Z", 1)){                                                                   │                                                                                              
                            fserror = ILLEGAL_MZ;                                                                                          │//  Printf("Test case 3, write Z first\n");                                                   
                    }                                                                                                                      │//  write_file_at(f, "Z", 1, BEGINNING_OF_FILE+1, 0L);                                        
            }                                                                                                                              │//  fs_print_error();                                                                         
    }                                                                                                                                      │//  write_file_at(f, "M", 1, BEGINNING_OF_FILE, 0L);                                          
                                                                                                                                           │//  fs_print_error();                                                                         
    else if (offset == 1L){                                                                                                                │                                                                                              
                                                                                                                                           │                                                                                              
            if(!strncmp(((char*)data), "Z", 1)){                                                                                           │                                                                                              
                    if(offset == 0L && !strncmp(((char*)data), "M", 1)){                                                                   │  printf("Closing file...\n");                                                                
                            fserror = ILLEGAL_MZ;                                                                                          │  close_file(f);                                                                              
                    }                                                                                                                      │  fs_print_error();                                                                           
            }                                                                                                                              │  return 0;                                                                                   
    } 

这是我尝试过的另一件没有运气的事情(至少它符合哈哈哈):

 char *ptr = NULL;                                                                                                                      │                                                                                                    char *buffer = (char*) data;                                                                                                           


 read_file_from(file, ptr, 2, BEGINNING_OF_FILE, 0L);                                                                                   │  printf("Test case: mz is lower cased\n");                                                   
 char *buffer2 = (char*) ptr;//data already written in file                                                                             │  write_file_at(f, "mz", 2, BEGINNING_OF_FILE, 0L);                                           
                                                                                                                                           │  fs_print_error();                                                                           
                                                                                                                                           │  printf("\n\n");                                                                             
  if(buffer2[0] == 'M'){                                                                                                                 │                                                                                              
          if(buffer[1] == 'Z'){                                                                                                          │                                                                                              
          fserror = ILLEGAL_MZ;                                                                                                          │ Test Case                                                                                  
          }                                                                                                                              │  printf("Test case: write Z in the second byte first, then write M in the first byte\n");    
  }                                                                                                                                      │  write_file_at(f, "Z", 1, BEGINNING_OF_FILE, 1L);                                            
  if(buffer2[0] == 'M'){                                                                                                                 │  fs_print_error();                                                                           
          if(buffer[1] == 'Z'){                                                                                                          │  write_file_at(f, "MZ", 2, BEGINNING_OF_FILE, 0L);                                           
          fserror = ILLEGAL_MZ;                                                                                                          │  fs_print_error();                                                                           
          }                                                                                                                              │  printf("\n\n");                                                                             
  }       

*此处还注意到read_file_from是我教授给出的方法,这里是参数:

unsigned long read_file_from(文件文件,void * data,unsigned long num_bytes,SeekAnchor start,long offset)

非常感谢任何帮助,谢谢!!

BTW,Linux用户永远不应该喝酒和root!

1 个答案:

答案 0 :(得分:1)

要做得更清楚

if(buff[0] == 'M' && buff[1] == 'Z')....

这是惯用的C方式。既然你不提出实际问题就很难给出更好的答案