Strings Format in C #include <stdio.h> Decode Lab Your assignment is to write a
ID: 3710053 • Letter: S
Question
Strings Format in C
#include <stdio.h>
Explanation / Answer
As per your requirement the below one is solution please follow it
decode.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void handle_line(char *line) {
printf("%s", line); //the line got the file
}
char* handle_line_picked(char *line) {
return line; //not completed but theres the structure
}
char* getCode(){
int size = 53, pos; //53 chars taking in account the NULL at the end
int c;
char *buffer = (char *)malloc(size);
FILE *f = fopen("codefile.txt", "r");
if(f) {
do { // read all lines in file (should be only one, the code...)
pos = 0;
do{ // read one line
c = fgetc(f);
if(c != EOF) buffer[pos++] = (char)c;
if(pos >= size - 1) { // increase buffer length - leave room for 0
size *=2;
buffer = (char*)realloc(buffer, size);
}
}while(c != EOF && c != ' ');
buffer[pos] = 0;
// line is now in buffer
// handle_line(buffer);
} while(c != EOF);
fclose(f);
}
char *code =(char *)malloc(size);
strcpy(code,buffer );
free(buffer);
return code;
}
char* getMessage(){
int size = 53, pos; //53 chars taking in account the NULL at the end
int c;
char *buffer = (char *)malloc(size);
FILE *f = fopen("msgfile.txt", "r");
if(f) {
do { // read all lines in file (should be only one, the code...)
pos = 0;
do{ // read one line
c = fgetc(f);
if(c != EOF) buffer[pos++] = (char)c;
if(pos >= size - 1) { // increase buffer length - leave room for 0
size *=2;
buffer = (char*)realloc(buffer, size);
}
}while(c != EOF && c != ' ');
buffer[pos] = 0;
// line is now in buffer
handle_line(buffer);
} while(c != EOF);
fclose(f);
}
char *code =(char *)malloc(size);
strcpy(code,buffer );
free(buffer);
return code;
}
int getMessageSize(){
int size = 53, pos; //53 chars taking in account the NULL at the end
int c;
char *buffer = (char *)malloc(size);
int m_size = 0;
FILE *f = fopen("msgfile.txt", "r");
if(f) {
do { // read all lines in file (should be only one, the code...)
pos = 0;
do{ // read one line
c = fgetc(f);
if(c != EOF) buffer[pos++] = (char)c;
if(pos >= size - 1) { // increase buffer length - leave room for 0
size *=2;
buffer = (char*)realloc(buffer, size);
}
}while(c != EOF && c != ' ');
buffer[pos] = 0;
// line is now in buffer
//handle_line(buffer);
m_size = m_size +1;
} while(c != EOF);
fclose(f);
}
return m_size;
}
char* getMessageLine(int chosen){
int size = 53, pos; //53 chars taking in account the NULL at the end
int c;
char *buffer = (char *)malloc(size);
char *picked_line = (char *)malloc(size);
int m_size = 0;
FILE *f = fopen("msgfile.txt", "r");
if(f) {
do { // read all lines in file (should be only one, the code...)
pos = 0;
do{ // read one line
c = fgetc(f);
if(c != EOF) buffer[pos++] = (char)c;
if(pos >= size - 1) { // increase buffer length - leave room for 0
size *=2;
buffer = (char*)realloc(buffer, size);
}
}while(c != EOF && c != ' ');
buffer[pos] = 0;
// line is now in buffer
m_size = m_size +1;
if(m_size == chosen){
handle_line(buffer);
picked_line = handle_line_picked(buffer);
}
} while(c != EOF);
fclose(f);
}
return picked_line;
}
int main(int argc, char *argv[]) {
int size = 53; //53 chars taking in account the NULL at the end
//here i will save my secret code
char *code =(char *)malloc(size);
code = getCode();
printf("%s", "got in my buffer the needed code to decode ");
printf("%s", code);
printf("%s", " ");
//here i will save my secret message
char *message =(char *)malloc(size);
printf("%s", "all message lines ");
message = getMessage();
printf("%s", " ");
//the problem is you need some kind of way to read an specific line,
// moreover a way to count the lines on the message
int m_size; // size of the message
printf("%s", "all message lines count ");
m_size = getMessageSize();
printf("%d", m_size);
printf("%s", " ");
//if you want an specific line of the message, you can iterate over MessageLine
char *messageLine =(char *)malloc(size);
printf("%s", "selected Line 12 ");
messageLine = getMessageLine(12);
printf("%s", " ");
return 0;
}
------------- decodefile
CFL2AP(T9W,H!JEM86NDOBZ7UK1Y3VG4XR)ISOQ5. ;-
--------------- msgfile.txt
19546
14501
17309
13027
16149
21512
18035
14014
15700
12515
16514
18207
13407
14837
16842
21037
15333
12244
21224
16321
14146
16014
20727
12804
18811
13711