Prevent characters from being written out in file in c

407 views Asked by At

I'm trying to write a program in c to write out some strings, words, characters,etc... The gist of the program is to allow everything to be written, however the first two bytes of the program cannot contain the character "MZ". I'm new to c and pointers, here is what I've tried:

Method 1: This gave me compiling errors....

    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;                                                                                   
    } 

Here is another thing I've tried with no luck(well at least it complied hahaha):

 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");                                                                             
  }       

*also noted here that the read_file_from is a method given by my professor, here is the parameters:

unsigned long read_file_from(File file, void *data, unsigned long num_bytes, SeekAnchor start, long offset)

Any help would be greatly appreciated, thank you!!

BTW, Linux users should never drink and root!

1

There are 1 answers

0
pm100 On

its much clearer to do

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

This is the idiomatic C way to do it. Since you dont give your actual problems it hard to give a better answer