Someone ask me to help them extract their pictures from a Web Shots image collection file (.WBC). I tried XnView but it did not work. How can I do this in C?
From Mike:
I hacked together some code to do the job. Here it is. It's not production quality code, so if you do not understand it then do not run it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void save_image(const char* filename, FILE* in_fp)
{
char buf[4096];
size_t read;
FILE *fp;
fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "cannot open file.");
exit(1);
}
do {
read = fread(buf,1,sizeof(buf),in_fp);
fwrite(buf, 1, read, fp);
} while (read);
fclose(fp);
}
int main(int argc, char* argv[])
{
char buf[4096];
unsigned int read, read_tot = 0;
FILE *fp;
int image_count = 1;
char filename[255];
unsigned int i;
char pattern[] = "JFIF";
int pi = 0;
long int curpos;
char pad[50];
char src_filename[] =
"C:\\Documents and Settings\\mikeking\\Desktop\\WBC\\"
"Custom - CATHYS WEDDING.wbc";
char des_directory[] = "C:\\Documents and Settings\\mikeking\\Desktop\\F\\";
fp = fopen(src_filename, "rb");
if (!fp) {
fprintf(stderr, "cannot open file.");
exit(1);
}
do {
read = fread(buf,1,sizeof(buf),fp);
for(i=0; i<read; i++){
if (buf[i] == pattern[pi]) {
pi++;
if (pi == sizeof(pattern)) {
strcpy(filename, des_directory);
itoa(image_count, pad, 10);
image_count++;
strcat(filename, pad);
strcat(filename, ".jpg");
curpos = ftell(fp);
fseek(fp,read_tot+i-10,SEEK_SET);
save_image(filename,fp);
fseek(fp,curpos,SEEK_SET);
}
} else {
pi = 0;
}
}
read_tot += read;
} while (read);
fclose(fp);
return 0;
}
Related
i write this program for encrypt any file with any size but if file will smaller than 1Kbyte my program give me segment fault error whats wrong?
#include <stdio.h>
#include <stdlib.h>
long int findsize(char file_name[])
{
FILE* fp = fopen(file_name, "r");
if (fp == NULL) {
printf("File Not Found!\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
long int res = ftell(fp);
fclose(fp);
return res;
}
int main ()
{
FILE *fptr;
char path[256];
char* data;
int passcode;
printf("Enter the path of file : ");
scanf("%s",path);
long int file_size = findsize(path);
data = malloc(file_size);
fptr = fopen(path,"rb");
int i = 0;
while (!feof(fptr))
data[i++] = fgetc(fptr);
fclose(fptr);
fptr = fopen(path, "wb");
for (int j=0; j<i-1; j++)
fputc((data[j] ^ 0x60), fptr);
fclose(fptr);
free(data);
return 0;
}
my program can encrypt files bigger than 1GByte but. what should i do?
When I try to open a file "canbus.txt.txt" it is coming back with an error message that reads out "error: No error" repeatedly. I cannot find where this issue would be coming from. My file is in the main project directory and the name and extensions are all correct.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int i;
char value;
char test[60];
char *line;
int num = 0;
int address[25];
int values[25];
void ms_delay (int N)
{
T1CON = 0x08030;
int delay = N * 62.5; // 1 Milisecond => .001 / (256 * (1/16,000,000)) = 62.5
TMR1 = 0;
while (TMR1 < delay);
}
int main (void)
{
PIN_MANAGER_Initialize();
UART1_Initialize();
ECAN1_Initialize();
ECAN1_ReceiveEnable();
INTERRUPT_Initialize();
FILE *fp;
char* filename = "canbus.txt.txt";
fp = fopen(filename, "r");
if(fp == NULL){
perror("Error");
exit(EXIT_FAILURE);
}
while(fgets(line, sizeof(line), fp)){
fscanf(fp, "%lf %lf", &address[num], &values[num]);
sprintf(test, "Value = %lf Other = %lf", address[num], values[num]);
int i = 0;
while(test[i] != '\0'){
UART1_Write(test[i]);
i++;
}
++num;
}
ms_delay(250);
UART1_Write(0x0D);
UART1_Write(0x0A);
fclose(fp);
return 0;
}
#include <stdio.h>
int main()
{
FILE *fr;
char c;
fr = fopen("prog.txt", "r");
while( c != EOF)
{
c = fgetc(fr); /* read from file*/
printf("%c",c); /* display on screen*/
}
fclose(fr);
return 0;
}
To know more https://www.codesdope.com/c-enjoy-with-files/
I compile the code then run it but it returns "Error in writing encrypted data to file. So I assume the issue is somewhere in fwrite.
But I cannot pinpoint it.
I need some help here if you could explain the problem that would be very helpful thanks a lot
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int encrypt_data(FILE *);
int main(void)
{
FILE *fp;
int return_code;
printf("Please enter the file to be encrypted: ");
char filename[200];
scanf("%s", filename);
fp=fopen(filename,"r");
return_code = encrypt_data(fp);
return 0;
}
int encrypt_data(FILE *disk_fp)
{
int i;
unsigned long int file_size;
int key_length;
char *file_buff = NULL;
char key[] = "12`3-vk0fn";
key_length = strlen(key);
fseek(disk_fp, 0, SEEK_END);
file_size = ftell(disk_fp);
rewind(disk_fp);
file_buff = malloc(file_size);
if( fread(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in reading file\n");
return -1;
}
for( i=0; i<file_size; i++)
{
file_buff[i] = file_buff[i] ^ key[i%key_length];
}
rewind(disk_fp);
if( fwrite(file_buff, file_size, 1, disk_fp) != 1)
{
printf("Error in writing encrypted data to file\n");
return -1;
}
free(file_buff);
fclose(disk_fp);
return 0;
}
You are opening file with "r" mode which means readonly. Then you try write something to it. By the way you don't check that you open file without errors and don't close it when fwrite/fread failed.
I was wondering how I can get this code to overwrite a textfile from it's text value to it's ASCII value.
I want it to do something like this:
CMD > c:\users\username\desktop>cA5.exe content.txt
content.txt has "abc" in it and I want the command line to change the "abc" to it's ASCII values. 97... etc. I don't want anything written in the command window, I want it to change in the text file. Is this possible, if so, how could I do it with this existing code?
#include <stdio.h>
#include <stdlib.h>
int main(int argc[1], char *argv[1])
{
FILE *fp; // declaring variable
fp = fopen(argv[1], "rb");
if (fp != NULL) // checks the return value from fopen
{
int i;
do
{
i = fgetc(fp); // scans the file
printf("%c",i);
printf(" ");
}
while(i!=-1);
fclose(fp);
}
else
{
printf("Error.\n");
}
}
Not the best code but very simple.
#include <stdio.h>
#include <stdlib.h>
void convertToAHex(char *data, long int size, FILE *file){
rewind(file);
int i;
for(i = 0; i < size; ++i){
fprintf(file, "%d ", data[i]);
}
}
int main(int argc, char *argv[]){
if(argc != 2){
return EXIT_FAILURE;
}
FILE *file = fopen(argv[1], "r+");
if(file){
char *data;
long int size;
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
data = (char *) calloc(size, sizeof(char));
if(data){
fread(data, 1, size, file);
convertToAHex(data, size, file);
free(data);
}
fclose(file);
}
return EXIT_SUCCESS;
}
I am working on a project where I need to read data from a binary file. I am trying to store the data into a char buffer. Suppose the binary file consisted of a character, an int and a double what size would the char buffer need to be ? And how would I convert back into int's and doubles ?
I am reading the data into a char buffer because it would improve the speed of my program.
Thanks!
The following example program fread()s the first DATASIZE sets of a char, an int and a float from a file specified on the command line:
typedef struct Data_s {
char c;
int i;
float f;
} Data_t;
#define DATASIZE 3
int main(int argc, char ** argv) {
if (1 >= argc) {
fprintf(stderr, "usage: %s <file name>\n", argv[0]);
return EXIT_SUCCESS;
}
{
FILE * f = fopen(argv[1], "r");
if (!f) {
perror("fopen() failed.");
return EXIT_FAILURE;
}
{
Data_t data[DATASIZE];
size_t sizeData = sizeof(*data);
size_t sizeToRead = sizeof(data)/sizeData;
memset(data, 0, sizeToRead * sizeData);
size_t sizeRead = fread(&data, sizeData, sizeToRead, f);
if (0 != fclose(f))
perror("fclose() failed,");
if (sizeToRead != sizeRead) {
perror("fread() failed.");
return EXIT_FAILURE;
}
for (size_t i = 0; i < sizeToRead; ++ i)
printf("read c=0x%02hhx, i=%d, f=%f from '%s'\n", data[i].c, data[i].i, data[i].f, argv[1]);
}
}
return EXIT_SUCCESS;
}
You can use the fscanf function to read the data from the file straight into eagerly awaiting variables:
char c;
int i;
double d;
FILE *fp = fopen("C:\\example.txt", "rb");
if (fp)
{
fscanf(fp, "%c%d%lf", &c, &i, &d);
fclose(fp);
printf("Character: %c\nInteger: %d\nDouble: %lf\n", c, i, d);
}
EDIT: If you're looking for more info on fscanf, see here
EDIT2: Binary Solution
FILE *fp = fopen("C:\\example.txt", "rb");
if (fp)
{
char buffer[sizeof(int) + sizeof(double) + sizeof(char)];
if (fread(buffer, 1, sizeof(buffer), fp) == sizeof(buffer))
{
char c = *(char*)buffer;
int i = *(int*)(buffer + sizeof(char));
double d = *(double*)(buffer + sizeof(char) + sizeof(int));
}
fclose(fp);
}