-1

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!

Nam Vu
  • 1,727
  • 1
  • 11
  • 24
  • You might like the operator `||`, it wouls allow you to simplify your logic. – Yunnosch Sep 05 '17 at 16:28
  • 1
    for the first one you have to include the compiler error. For the second one you have to say what didnt work – pm100 Sep 05 '17 at 16:28
  • The first argument to `strncmp` must be `char *`. `((char *)data)[0]` is a `char`. – Barmar Sep 05 '17 at 16:30
  • Why on *earth* are you using `strcmp`, which expects *both* the `src` and `dst` to be *nullchar terminated strings* ? Would it be easier to simply compare the first octet against `'M'`, and the second against `'Z'` ? And `read_file_from(file, ptr, 2, BEGINNING_OF_FILE, 0L);` may compile, but I cannot fathom how sending a null pointer by-value as the second argument is going to do *anything* helpful, and considering `char *buffer2 = (char*) ptr;` you're of the belief `ptr` will change in that `read_file_from` call; it won't, so that code is a recipe for null-pointer dereferencing. – WhozCraig Sep 05 '17 at 16:35
  • I'm new to c, could you show an example of what this code should look like to compare the first character to "M"? – Nam Vu Sep 05 '17 at 17:00
  • You can write ***if(*data =='M')*** to compare the first element of data to 'M' character – Math Lover Sep 06 '17 at 08:33

1 Answers1

1

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

pm100
  • 48,078
  • 23
  • 82
  • 145