We are trying to convert an image into binary data and vice-versa for a project using C programming. All the other solutions we found on the net are either in C++ or Java. Here is the approach we tried:
Convert the image into a text file containing binary data. Each 8 characters corresponds to the character byte when the image is opened using a text editor.
Then we try to reconvert the binary data into its respective characters using a C program.
Then we open the result using Picasa Photoviewer. We get an invalid image.
How do we get back the original image? Here is the code we used to convert the image into a text file:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
FILE *fptr;
FILE *txt;
int c;
fptr=fopen("D:\\aa.bmp","r");
txt=fopen("D:\\test1.txt","w");
if(fptr==NULL)
{
printf("NOTHING In FILE");
fclose(fptr);
}
else
{
printf("success");
do
{
c=fgetc(fptr);
for(int i=0;i<=7;i++)
{
if(c&(1<<(7-i)))
{
fputc('1',txt);
}
else
{
fputc('0',txt);
}
}
// fprintf(txt,"\t");
}while(c!=EOF);
}
fclose(fptr);
fclose(txt);
printf("writing over");
getch();
}
Here is the code to convert the resulting text file to image file full of binary characters, i.e. a text file with only ones and zeroes.
#include<stdio.h>
#include<conio.h>
\\The following function converts the ones and zeroes in the text file into a character.
\\For example the text file may have the 8 consecutive characters '1','0','0','0','1','0','0','0'.
\\This converts it into the character equivalent of the binary \\value 10001000
char bytefromtext(char* text)
{
char result=0;
for(int i=0;i<8;i++)
{
if(text[i]=='1')
{
result |= (1<< (7-i) );
}
}
return result;
}
void main()
{
clrscr();
FILE *pfile;
FILE *image;
char buf[8];
char c;
int j=0;
image=fopen("D:\\aa2.bmp","w"); //open an empty .bmp file to
//write characters from the source image file
pfile=fopen("D:\\test1.txt","r");
if(pfile==NULL)
printf("error");
else
{
c=fgetc(pfile);
while(c!=EOF)
{
buf[j++]=c;
if(j==8)
{
fputc(bytefromtext(buf),image);
j=0;
}
c=fgetc(pfile);
}
fclose(pfile);
fclose(image);
}
getch();
}
We get an invalid image when the characters are written into the .bmp file. When we open this new file using a text editor and also the image file using a text editor we get the same characters.
Image to text file
fptr=fopen("D:\\aa.bmp","r");
The BMP file must be opened in binary mode ("rb") to ensure that the bytes values are read correctly. The "r" mode opens the file in text mode which may cause some characters to be converted, resulting in corrupted output.
For example, on Windows (or at least DOS), line endings will be converted from "\r\n" to "\n" and the character "\x1a" might be interpreted as and EOF indicator and truncate your input.
On UNIX-like systems, on the other hand, there is no difference.
do
{
c=fgetc(fptr);
for(int i=0;i<=7;i++)
{
/* ... */
}
// fprintf(txt,"\t");
}while(c!=EOF);
This loop is completely wrong. You need to check for EOF at the top of the loop. When fgetc() returns EOF, your code will take the EOF value (typically -1), and output the corresponding ones and zeroes, before exiting the loop. This will also corrupt your output.
Instead, you should do something like this:
while ((c = fgetc (fptr)) != EOF) {
{
/* ... */
}
If you're uncomfortable with assignment and comparison in the same expression, there is a fix for that:
while (1)
{
c = fgetc (fptr);
if (c == EOF)
break;
/* ... */
}
Note also that fgetc() also returns EOF on error. You should test for this (if (ferror (fptr))) and report the problem to the user.
fclose(fptr);
fclose(txt);
You should check the return value of fclose() and report any error back to the user, at least on the output stream. On some file systems, the last output will not be written to disc until the stream is closed, and any error writing it will be reported by fclose(). See "What are the reasons to check for error on close()?" for a illuminating tale of what can happen when you don't.
Text file to image
image=fopen("D:\\aa2.bmp","w"); //open an empty .bmp file to
You must use binary mode ("wb") as explained above.
char bytefromtext(char* text)
{
char result=0;
for(int i=0;i<8;i++)
{
if(text[i]=='1')
{
result |= (1<< (7-i) );
}
}
return result;
}
You should use unsigned char when dealing with binary data. Plain char is either signed or unsigned at the choice of the implementor (eg. compiler vendor).
If the value stored in result cannot be represented in a signed char (such as 1<<7), the result is implementation defined. This could theoretically corrupt your output. Although I think your code will probably work as you intended in most cases, you should still use unsigned char as a matter of principle.
(This assumes, of course, that char is not larger than 8 bits which is usually the case.)
char c;
/* ... */
c=fgetc(pfile);
while(c!=EOF)
{
/* ... */
c=fgetc(pfile);
}
This loop is wrong for another reason. If plain char happens to be unsigned, c will never compare equal to EOF which always has a negative value. You should use an int variable, test against EOF, and only then use the value as a character value.
fclose(pfile);
fclose(image);
You should check the return values as mentioned above.
Other issues
I also have a couple of other quibbles with your code.
In C, main() always returns int, and you should return an appropriate value to indicate success or failure. (This does not apply to a freestanding environment, eg. a C program running without an operating system.)
The comment section in the second program has backslashes instead of forward slashes. When posting code, you should always copy/paste it to avoid introducing new errors.
Check your write mode for image=fopen("D:\\aa2.bmp","w"); its not in binary, open it in "wb".
This is code that works good. Tryed on raspberryPi3 and gcc.
bmp to text
#include <stdio.h>
int main(int argc, char*argv[]){
FILE *ptr_bmp_in;
FILE *ptr_text_out;
int c;
ptr_bmp_in=fopen("panda_input.bmp","rb");
ptr_text_out=fopen("panda_to_text.txt","w");
if(!ptr_bmp_in)
{
printf("Unable to open file\n");
return 1;
}
while((c=fgetc(ptr_bmp_in)) != EOF)
{
for(int i=0;i<=7;i++)
{
if(c&(1<<(7-i)))
{
fputc('1',ptr_text_out);
}
else
{
fputc('0',ptr_text_out);
}
}
}
fclose(ptr_bmp_in);
fclose(ptr_text_out);
printf("Writing done\n");
return 0;
}
and text to bmp
#include <stdio.h>
char bytefromtext(unsigned char* text)
{
char result = 0;
for(int i=0;i<8;i++)
{
if(text[i]=='1')
{
result |= (1 << (7-i));
}
}
return result;
}
int main(int argc, char*argv[]){
FILE *ptr_txt_in;
FILE *ptr_bmp_out;
unsigned char buf[8];
int c;
int j = 0;
ptr_txt_in=fopen("panda_to_text.txt","r");
ptr_bmp_out=fopen("panda_output.bmp","wb");
if(!ptr_txt_in)
{
printf("Unable to open file\n");
return 1;
}
while((c=fgetc(ptr_txt_in)) != EOF)
{
buf[j++] = c;
if(j==8)
{
fputc(bytefromtext(buf),ptr_bmp_out);
j=0;
}
}
fclose(ptr_txt_in);
fclose(ptr_bmp_out);
printf("Writing done\n");
return 0;
}
Related
I need to write into a file with uppercase some strings ,then to display on screen with lowercase. After that ,I need to write into file the new text (lowercase one). I write some code ,but it doesn't work. When I run it , my file seems to be intact and the convert to lowercase don't work
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void main(void) {
int i;
char date;
char text[100];
FILE *file;
FILE *file1;
file = fopen("C:\\Users\\amzar\\Desktop\\PC\\Pregatire PC\\Pregatire PC\\file\\da.txt","r");
file1 = fopen("C:\\Users\\amzar\\Desktop\\PC\\Pregatire PC\\Pregatire PC\\file\\da.txt","w");
printf("\nSe citeste fisierul si se copiaza textul:\n ");
if(file) {
while ((date = getc(file)) != EOF) {
putchar(tolower(date));
for (i=0;i<27;i++) {
strcpy(text[i],date);
}
}
}
if (file1) {
for (i=0;i<27;i++)
fprintf(file1,"%c",text[i]);
}
}
There are several problems with your program.
First, getc() returns int, not char. This is necessary so that it can hold EOF, as this is not a valid char value. So you need to declare date as int.
When you fix this, you'll notice that the program ends immediately, because of the second problem. This is because you're using the same file for input and output. When you open the file in write mode, that empties the file, so there's nothing to read. You should wait until after you finish reading the file before you open it for output.
The third problem is this line:
strcpy(text[i],date);
The arguments to strcpy() must be strings, i.e. pointers to null-terminated arrays of char, but text[i] and date are char (single characters). Make sure you have compiler warnings enabled -- that line should have warned you about the incorrect argument types. To copy single characters, just use ordinary assignment:
text[i] = date;
But I'm not really sure what you intend with that loop that copies date into every text[i]. I suspect you want to copy each character you read into the next element of text, not into all of them.
Finally, when you were saving into text, you didn't save the lowercase version.
Here's a corrected program. I've also added a null terminator to text, and changed the second loop to check for that, instead of hard-coding the length 27.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void main(void) {
int i = 0;
int date;
char text[100];
FILE *file;
FILE *file1;
file = fopen("C:\\Users\\amzar\\Desktop\\PC\\Pregatire PC\\Pregatire PC\\file\\da.txt","r");
printf("\nSe citeste fisierul si se copiaza textul:\n ");
if(file) {
while ((date = getc(file)) != EOF) {
putchar(tolower(date));
text[i++] = tolower(date);
}
text[i] = '\0';
fclose(file);
} else {
printf("Can't open input file\n");
exit(1);
}
file1 = fopen("C:\\Users\\amzar\\Desktop\\PC\\Pregatire PC\\Pregatire PC\\file\\da.txt","w");
if (file1) {
for (i=0;text[i] != '\0';i++)
fprintf(file1,"%c",text[i]);
fclose(file1);
} else {
printf("Can't open output file\n");
exit(1);
}
}
I have to write a program witch reads from a file received by line and then it overwrites it with the read words uppercased.
This is my code
void toUpperCase(char* string) {
int i=0;
while(string[i])
{
string[i]=toupper(string[i]);
i++;
} }
int main(int argc, char** argv) {
if(argc==1)
{
puts("Error: INSERT PATH");
exit(0);
}
char* file=argv[1];
FILE* fd=fopen(file,"r+");
if(fd<0)
{
perror("Error opening file: ");
exit(0);
}
char buffer[30][30];
int i=0;
while(!feof(fd))
{
fscanf(fd,"%s",buffer[i]);
i++;
}
int j=0;
for(j=0; j<i; j++)
{
toUpperCase(buffer[j]);
fwrite(buffer[j],strlen(buffer[j]),1,fd);
}
fclose(fd);
return 0; }
but this program appends the words contained in buffer[][] instead of overwriting the file.
If the file contain was something like pippo pluto foo then, after the execution is pippo pluto fooPIPPOPLUTOFOO instead of PIPPO PLUTO FOO.
Where am i wrong? Thank you
You have to reset the file position indicator using fseek, as fscanf will advance it. Something like
fseek(fd, length_of_read_string, SEEK_CUR);
This allows you to read the file in chunks, but it will be tricky to get right. Or of course reset it to the file start because you read everything in 1 go:
fseek(fd, 0L, SEEK_SET);
I strongly recommend writing the modified data into a new file, and then after the program has run, delete the initial file and rename the new one. That will also take care of another issue with your program, you are reading the entire file into memory before handling it.
If you want to do in-place translation that doesn't change lengths, you can open the source file in two streams and then do read-chunk, write-chunk in lockstep. That has the advantage of being super-easy to convert to a non-in-place version that will work with nonseekable files too (stdin/stdout, pipes, and sockets).
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h> //toupper
inline void upcaseStr(char* str){
for(;*str;str++) { *str=toupper(*str); }
}
int upcaseStream(FILE* in, FILE* out){
char buf[BUFSIZ]; //BUFSIZ is an implementation-defined constant for an optimal buffer size
while(fgets(buf, BUFSIZ, in)){
upcaseStr(buf);
if(fputs(buf, out) == EOF){ return 1; }
}
if(!feof){ return 1; }
return 0;
}
int main(int argc, char **argv)
{
//default in and out
FILE* in = stdin;
FILE* out = stdout;
if(argc == 2) {
in = fopen(argv[1], "r"); //for reading
out = fopen(argv[1], "r+"); //for writing (and reading) starting at the beginning
if(!(in && out)){
fprintf(stderr, "Error opening file %s for reading and writing: %s\n", argv[1], strerror(errno));
}
}
return upcaseStream(in, out);
}
If you do use the in-place version, then in the unlikely event that the if(fputs(buf, out) == EOF){ return 1; } line should return, you're screwed unless you have a backup copy of the file. :)
Note:
You shouldn't name your FILE pointers fd because C people will tend to think you mean "file descriptor". FILE is a struct around a file descriptor. A file descriptor is just an int that you can use for FILE access with the raw system calls. FILE streams are an abstraction layer on top of file descriptors--they aren't file descriptors.
As you read from the file, its internal position indicator gets moved. Once you start writing, you start writing from that position on, which happens to be at the end of the file. So you effectively append the data to the file.
Rewind the handle to reset the position indicator before writing into the file:
rewind(fp);
On a side note, you are reading the file incorrectly:
while(!feof(fd))
{
fscanf(fd,"%s",buffer[i]);
i++;
}
When you reach the end of the file, fscanf will return an error and not read anything, yet you still increment variable i, as if the read was successful. And then you check feof() for end-of-file, but i was already incremented.
Check feof() and return of fscanf() immediately after calling fscanf():
while(1)
{
int read = fscanf(fd,"%s",buffer[i]);
if( read != 1 )
//handle invalid read
if( feof(fd) )
break;
i++;
}
Think about what happens if the string is longer than 29 characters and/or the file contains more than 30 strings. char buffer[30][30];
Welcome to StackOverflow!
Reopening the stream with fopen with the "w" parameter:
fd=fopen(file, "w");
It opens the file and if there are any contents in the file, it clears them.
I have to develop a program in C that can have two kinds of inputs.
By feeding it a string ( I am assuming like this filename < String1234455678, please correct me if I am wrong).
By reading data from some file(s).
I have to do some checks regarding the characters that are in it and store them in an array. But I want to learn how to use the getc() from stdin first.
My first question is, can I use getc() in both cases?
I wanted to loop through every single character in the feed line/file, and I assume the code would look something like this:
char Array1[];
char charHolder;
//If the file/feed has chars (!NULL), execute
if ((charHolder = getchar())!=NULL){
//Do something
//Do some more
//Finally append to Array1
Array1[] = charHolder;
}
There might be some issues with the code above. I wanted to know if that kind of inserting is valid in C (with no index specified, which it will just push the value at the end of the array). Also, I read from http://beej.us/guide/bgc/output/html/multipage/getc.html that getc(stdin) and getchar() are exactly equivalent. I just want to double check that this is indeed true and either function will work with both my cases where I have to read data (from a file and feeding my program a string).
Also, I was wondering how I can achieve reading characters from multiple files. Say if my program was to be executed as programName file1 file2.
Thank you for your time and help!
Cheers!
Edit 1:
I also wanted to know how to check when the chars end from a file/string feed. Should I use the EOF for both cases?
Example:
while ((charHolder = getchar()) != EOF){
//code
}
Here is a sample:
#include <stdio.h>
void do_read(FILE * file, int abort_on_newline) {
char ch;
while (1) {
ch = getc(file);
if (ch == EOF) {
break;
}
if (abort_on_newline && ch == '\n') {
break;
}
printf("%c", ch);
}
}
int main(int argc, char * argv[])
{
int i = 1;
FILE * fp = NULL;
if (1 == argc) {
// read input string from stdin, abort on new line (in case of interactive input)
do_read (stdin, 1);
}
else {
// cycle through all files in command line arguments and read them
for (i=1; i < argc; i++) {
if ((fp = fopen(argv[i], "r")) == NULL) {
printf("Failed to open file.\n");
}
else {
do_read(fp,0);
fclose(fp);
}
}
}
return 0;
}
Use it like this:
To read from stdin: echo youstring | youprogram, or just start
yourprogram to get input from user
To read from file(s) yourprogram yourfile1 yourfile2 ...
Yes your can use getc in both cases, yes you should check for EOF in both cases, except for interactiv input. In case of binary files you also need to use feof function to check for EOF. See code above to read from multiple files.
I used the code included below to decrypt some graphic files. I would like to modify it so it can encrypt them again. Unfortunately I'm a graphic designer and I only have very basic knowledge about programing (and not in C) so I don't understand this code on a level that would allow me to modify it by myself.
#include <stdio.h>
int main (int argc, char **argv)
{
FILE *inp, *outp;
int i;
char sig[] = "CF10", *ptr;
if (argc != 3)
{
printf ("usage: decode [input] [output]\n");
return -1;
}
inp = fopen (argv[1], "rb");
if (inp == NULL)
{
printf ("bad input file '%s'\n", argv[1]);
return -2;
}
ptr = sig;
while (*ptr)
{
i = fgetc (inp);
if (*ptr != i)
{
printf ("input file sig is not 'CF10'\n");
return -2;
}
ptr++;
}
outp = fopen (argv[2], "wb");
if (outp == NULL)
{
printf ("bad output file '%s'\n", argv[1]);
return -2;
}
do
{
i = fgetc(inp);
if (i != EOF)
fputc (i ^ 0x8d, outp);
} while (i != EOF);
fclose (inp);
fclose (outp);
printf ("all done. bye bye\n");
return 0;
}
This could, at best, be described as obscuring the content. It uses 'xor' encryption, and the one advantage of that is that is self-decrypting.
You can run the code on an unencrypted file and get an encrypted file, or on an encrypted file and get an unencrypted file.
I misread the code; sorry. Your code reads the CF10 magic number, and then writes the rest of the data after xor'ing it; the CF10 tells it that it is obscured. To obscure unobscured data, you should write the CF10 magic number to the output file, then read the input and xor it and write it. A refined version of the code would test the first four bytes of the file (for equality with CF10) to ensure that you are not re-obscuring and already obscured file.
You might need to handle an option argument that determines whether to encrypt or decrypt (obscure or unobscure). Alternatively, you can use the first four bytes of the file to tell you whether to encrypt or decrypt.
Note that the basic point of the original answer — that xor encryption and decryption are fundamentally the same operation — remains valid.
i am trying to read UTF8 text from a text file, and then print some of it to another file. I am using Linux and gcc compiler. This is the code i am using:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fin;
FILE *fout;
int character;
fin=fopen("in.txt", "r");
fout=fopen("out.txt","w");
while((character=fgetc(fin))!=EOF){
putchar(character); // It displays the right character (UTF8) in the terminal
fprintf(fout,"%c ",character); // It displays weird characters in the file
}
fclose(fin);
fclose(fout);
printf("\nFile has been created...\n");
return 0;
}
It works for English characters for now.
Instead of
fprintf(fout,"%c ",character);
use
fprintf(fout,"%c",character);
The second fprintf() does not contain a space after %c which is what was causing out.txt to display weird characters. The reason is that fgetc() is retrieving a single byte (the same thing as an ASCII character), not a UTF-8 character. Since UTF-8 is also ASCII compatible, it will write English characters to the file just fine.
putchar(character) output the bytes sequentially without the extra space between every byte so the original UTF-8 sequence remained intact. To see what I'm talking about, try
while((character=fgetc(fin))!=EOF){
putchar(character);
printf(" "); // This mimics what you are doing when you write to out.txt
fprintf(fout,"%c ",character);
}
If you want to write UTF-8 characters with the space between them to out.txt, you would need to handle the variable length encoding of a UTF-8 character.
#include <stdio.h>
#include <stdlib.h>
/* The first byte of a UTF-8 character
* indicates how many bytes are in
* the character, so only check that
*/
int numberOfBytesInChar(unsigned char val) {
if (val < 128) {
return 1;
} else if (val < 224) {
return 2;
} else if (val < 240) {
return 3;
} else {
return 4;
}
}
int main(){
FILE *fin;
FILE *fout;
int character;
fin = fopen("in.txt", "r");
fout = fopen("out.txt","w");
while( (character = fgetc(fin)) != EOF) {
for (int i = 0; i < numberOfBytesInChar((unsigned char)character) - 1; i++) {
putchar(character);
fprintf(fout, "%c", character);
character = fgetc(fin);
}
putchar(character);
printf(" ");
fprintf(fout, "%c ", character);
}
fclose(fin);
fclose(fout);
printf("\nFile has been created...\n");
return 0;
}
This code worked for me:
/* fgetwc example */
#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>
int main ()
{
setlocale(LC_ALL, "en_US.UTF-8");
FILE * fin;
FILE * fout;
wint_t wc;
fin=fopen ("in.txt","r");
fout=fopen("out.txt","w");
while((wc=fgetwc(fin))!=WEOF){
// work with: "wc"
}
fclose(fin);
fclose(fout);
printf("File has been created...\n");
return 0;
}
If you do not wish to use the wide options, experiment with the following:
Read and write bytes, not characters.
Also known as, use binary, not text.
fgetc effectively gets a byte from a file, but if the byte is greater than 127, try treating it as a int instead of a char.
fputc, on the other hand, silently ignores putting a char > 127. It will work if you use an int rather than char as the input.
Also, in the open mode, try using binary, so try rb & wb rather than r & w
The C-style solution is very insightful, but if you'd consider using C++ the task becomes much more high level and it does not require you to have so much knowledge about utf-8 encoding. Consider the following:
#include<iostream>
#include<fstream>
int main(){
wifstream input { "in.txt" }
wofstream output { "out.txt" }
// Look out - this part is not portable to windows
locale utf8 {"en_us.UTF-8"};
input.imbue(utf8);
output.imbue(utf8);
wcout.imbue(utf8);
wchar_t c;
while(input >> noskipws >> c) {
wcout << c;
output << c;
}
return 0;
}