我當前正試圖創建一個文件編碼器,它將幾行文本寫入.dat文件,並在運行可執行文件後輸出文件。但是,有一些錯誤使我無法編譯,我也不知道如何設置uint32keylocation。設置uint32關鍵位置並將其寫入文件時遇到問題
這裏是我的代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
/*
message pair structure
uint32 size
char * message
file spec:
char * Header = CS2060 with no null byte
uint32 keylocation
messagePair First Name
messagePair Last Name
messagePair Message 1 = what grade you think you should get in class
messagePair key
messagePair message 2 = go for lol points
No code needed for assignment, just send the output file (.dat or .bin is an appropriate file name extension)
*/
int xor(char * in, uint32_t insize, char * pass, uint32_t passSize){
uint32_t i;
for(i = 0; i < insize; i++){
in[i] = in[i]^pass[i%passSize];
}
return i;
}
int main(int argc, char ** argv){
if(argc != 2){
fprintf(stderr,"Usage: %s [filename]\n",argv[0]);
return 1;
}
FILE * fh = fopen(argv[1],"rb");
if(!(fh)){
fprintf(stderr,"Could not open file\n");
return 1;
}
char header[7];
uint32_t keyloc;
uint32_t fsize;
uint32_t lsize;
uint32_t keysize;
uint32_t m1size;
uint32_t m2size;
uint32_t realLocation;
char * first;
char * last;
char * key;
char * m1;
char * m2;
fwrite(header, 6, 1, fh);
if(strncmp(header,"CS2060",6)){
fprintf(stderr,"header is incorrect\n");
return 2;
}
fread(&keyloc,sizeof(uint32_t),1,fh);
char first[] = 'name';
uint32_t fsize = strlen(first);
fwrite(&fsize,sizeof(uint32_t),1,fh);
fwrite(first,fsize,1,fh);
fread(&keyloc,sizeof(uint32_t),1,fh);
char last[] = 'namee';
uint32_t fsize = strlen(last);
fwrite(&fsize,sizeof(uint32_t),1,fh);
fwrite(last,fsize,1,fh);
fread(&keyloc,sizeof(uint32_t),1,fh);
char m1[] = 'B';
uint32_t fsize = strlen(m1);
fwrite(&fsize,sizeof(uint32_t),1,fh);
fwrite(m1,fsize,1,fh);
fread(&keyloc,sizeof(uint32_t),1,fh);
char key[] = 'The key is H';
uint32_t fsize = strlen(key);
fwrite(&fsize,sizeof(uint32_t),1,fh);
fwrite(key,fsize,1,fh);
fread(&keyloc,sizeof(uint32_t),1,fh);
char m2[] = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstleyVEVO';
uint32_t fsize = strlen(m2);
fwrite(&fsize,sizeof(uint32_t),1,fh);
fwrite(m2,fsize,1,fh);
realLocation = ftell(fh);
fread(&keysize,sizeof(uint32_t),1,fh);
key = (char *)malloc(keysize+1);
fread(key,keysize,1,fh);
key[keysize] = 'H';
printf("First: %s\n",first);
printf("Last: %s\n",last);
printf("m1: %s\n",m1);
printf("m2: %s\n",m2);
if(realLocation == keyloc){
printf("The location is correct\n");
}else{
printf("The location is INCORRECT\n");
}
return 0;
}
這裏是顯示錯誤消息,這是相當長的,我在哪裏開始的損失。
new2.c: In function ‘main’:
new2.c:69:8: error: conflicting types for ‘first’
char *first[] = 'name';
^
new2.c:54:9: note: previous declaration of ‘first’ was here
char * first;
^
new2.c:69:18: warning: character constant too long for its type
char *first[] = 'name';
^
new2.c:69:2: error: invalid initializer
char *first[] = 'name';
^
new2.c:70:11: error: redeclaration of ‘fsize’ with no linkage
uint32_t fsize = strlen(first);
^
new2.c:47:11: note: previous declaration of ‘fsize’ was here
uint32_t fsize;
^
new2.c:70:26: warning: passing argument 1 of ‘strlen’ from incompatible pointer type
uint32_t fsize = strlen(first);
^
In file included from /usr/include/stdio.h:29:0,
from new2.c:1:
/usr/include/string.h:33:9: note: expected ‘const char *’ but argument is of type ‘char **’
size_t _EXFUN(strlen,(const char *));
這是我收到的每個寫入塊的錯誤。編輯2:好吧,所以在從頭開始重新構建之後,這就是我迄今爲止成功編寫的內容。所以我想我可以繼續使用這種格式來寫剩下的內容。我唯一的問題是什麼是uint32 keylocation,我該如何設置它?說明似乎含糊不清。還有我怎麼直接回複評論,但仍然包括我的代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
/*
message pair structure
uint32 size
char * message
file spec:
char * Header = CS2060 with no null byte
uint32 keylocation
messagePair First Name
messagePair Last Name
messagePair Message 1 = what grade you think you should get in class
messagePair key
messagePair message 2 = go for lol points
No code needed for assignment, just send the output file (.dat or .bin is an appropriate file name extension)
*/
int xor(char * in, uint32_t insize, char * pass, uint32_t passSize){
uint32_t i;
for(i = 0; i < insize; i++){
in[i] = in[i]^pass[i%passSize];
}
return i;
}
int main(){
FILE * fp;
fp = fopen("HW06.dat","w");
if(fp == NULL){
printf("Could not open file\n");
return 1;
}
char header[7] = "CS2060";
uint32_t keyloc;
uint32_t fsize;
uint32_t lsize;
uint32_t keysize;
uint32_t m1size;
uint32_t m2size;
uint32_t realLocation;
char * first;
char * last;
char * key;
char * m1;
char * m2;
fwrite(header,sizeof(char), 6, fp);
fclose(fp);
return 0;
}
好吧,有一個問題,_why你聲明變量twice_? – ameyCU
'char * first =「name」;'或'char first [] =「name」;'或'char * first;'then'first =「name」;' – BLUEPIXY
您似乎錯誤地編輯了我的答案而不是您的問題,朋友。 – Ian