gets after scanf issue + can’t read bytes using pointer returned from malloc?

Hello,

Jeeez guys, I don’t know how you guys do it! I got to tell ya, I am sort of struggling with C. I do some reading about C sample codes from books and the net, but I really find C quite difficult for me! Since I started with this language
I was never a fanatic of malloc since I use it rarely and only to store arrays of structs in heap. I am now experimenting with mallocing bytes only…. It shouldn’t be hard…right? but after tryng all morning here…. I give up … I need some guidence.
I hope someone can guide me as to what I am doing wrong.

First, there is two issues as I tried to describe in this thread’s title. Before I talk about the two issues, it is important to get an idea of what I am trying to do. The following code is a quick side utility I use to do some conversions
for my LCD’s pixel resolutions. So here is a snipet of what worked for years.

#include 
#include 
#include 
using namespace std;
  
unsigned int o[69750]; 
unsigned int r[69696]; 
unsigned int st[69696]; 
unsigned int w[46464]; 

void f1(unsigned int WINCOLS, unsigned int COLUMNS, unsigned int ROWS) 
{
FILE *fp;    
long s,PIXELSTOCONVERT=0; 
int x;

char str_s[80];   
char str_t[80];   

printf("Please enter source file with path n");
gets(str_s);

//Open file for reading 
if((fp = fopen(str_s, "rb")) == NULL){
printf("Cannot open file. n");
printf("nPlease type -e- and hit -enter key- to exit this utility: ");
cin >> x;
exit(1);
}

printf("Please enter target file with path n");
gets(str_t);
printf("Please enter # of pixels to convertn");
scanf("%d", &PIXELSTOCONVERT); //Input!
printf("READING SOURCE DATA! PLEASE WAIT!nn");

PIXELSTOCONVERT = PIXELSTOCONVERT *3; 

// Read original bmp file with signature
for(s=0;s<(PIXELSTOCONVERT+54);s++)
{
//Read each element from original bmp source file into o array
if(fread(&o[s],1,1, fp) != 1) {  
printf("Read error occured. n"); 
printf("nPlease type -e- and hit -enter key- to exit: ");
cin >> x;
exit(1);
}
}

fclose(fp); //Close the file stream!

// ... other code ...
}

void main()
{
f1(528, 176, 132); 
}

Basically,  the above snipet simply passes in 3 parameters to f1() and then asks 3 questions which are, the source file name, the target file name and the number of pixels to convert. The number of pixles to convert is multiplied by 3 since the
source file is a Windows .bmp rgb 3 bytes per pixel (888) format. Then the source file is read inot the o[] buffer one byte at a time. The program goes on with the conversion logic which is not really relevant to this post.

All of this always worked, but now I would like to make the utility it a little more user freindly. As I mentioned before that there are two issues in this thread.

The first, I would like to offer several options to the user so he can select different parameters before the f1() function is called. And the second issue is that I would like to replace those huge arrays by using malloc instead.

So here is the snipet that doesn’t work! 

#include 
#include 
#include 
using namespace std;
  
//unsigned int o[69750]; 
unsigned int r[69696]; 
unsigned int st[69696]; 
unsigned int w[46464]; 

void f1(unsigned int WINCOLS, unsigned int COLUMNS, unsigned int ROWS, unsigned int *xo)
{
FILE *fp;    
long s,PIXELSTOCONVERT=0; 
int x;
char str_s[80];   
char str_t[80];   

printf("Please enter source file with path n");
gets(str_s);

//Open file for reading 
if((fp = fopen(str_s, "rb")) == NULL){
printf("Cannot open file. n");
printf("nPlease type -e- and hit -enter key- to exit this utility: ");
cin >> x;
exit(1);
}

printf("Please enter target file with path n");
gets(str_t);
printf("Please enter # of pixels to convertn");
scanf("%d", &PIXELSTOCONVERT); 
printf("READING SOURCE DATA! PLEASE WAIT!nn");

PIXELSTOCONVERT = PIXELSTOCONVERT *3; 

// Read original bmp file with signature
for(s=0;s<(PIXELSTOCONVERT+54);s++)
{
//Read each element from original bmp source file into o array
if(fread(xo,1,1, fp) != 1) {   
printf("Read error occured. n"); 
printf("nPlease type -e- and hit -enter key- to exit: ");
cin >> x;
exit(1);
}
}

fclose(fp); //Close the file stream!

// ... other code ...
}


void main()
{
int i =1;
unsigned int *o;
unsigned int winbmpSignature = 54;
char str_s[80];

printf("Please select one of the following memory block sizes to convert. nnn");

printf(" ~~~~~~~~~~~~~~~ FIXED LCD MEMORY BLOCKS n");
printf(" #1) LCD frame 1.5 inch (176 x 132) 46K memory bock.n");
printf(" #2) LCD frame 2.0 inch (176 x 220) 78K memory bock.n");
printf(" #3) LCD frame 3.0 inch (320 x 240) 154K memory bock.nnn");
printf(" ~~~~~~~~~~~~~~ OTHER MEMORY BLOCK SIZES n");
printf(" #4) small (176 x 186) 64K  -1-memory block.n");
printf(" #5) medium (250 x 260) 130K -2-memory blocks.n");
printf(" #6) large (320 x 300) 192K -3-memory blocks.n");
printf(" #7) huge  (350 x 350) 245K -4-memory blocks.n");

printf("n n n");
printf(" >: ");

scanf("%d", &i);
if(gets(str_s)==NULL) {}
switch(i)
{
case 1:
o = (unsigned int*) malloc((176*3*132)+ winbmpSignature);
f1(528, 176, 132, o); 
break;
 
case 2:
//...
break;
}
}

 First, using scanf() before gets() causes the logic to skip the gets() function.  So I get around this with the following line:

if(gets(str_s)==NULL) {}

which is probably an unethical programming tactic ….  and I don’t even know why it works!!!! Am I clearing the gets() buffer ?

Many articles say to use fgets() instead. But fgets() requires to use a stream …  why use a stream just to get a string ??? Seems to be alot of overhead when gets() always did work for me… I could be wrong! Honestly, I don’t know what else to do
with this.

Secondly,  when I get passed the 3 questions and want to read the source file in the buffer o[] one byte at a time.. it doesn’t work….

I tried many variations of fread like:

if(fread(xo,1,1, fp) != 1)  

or

if(fread(xo[s],1,1,fp) != 1)

How can I read in one byte at a time like I used to… but instead of reading it in an array like I used to, I would read it in the block of memory set asside by malloc?

I have seen samples that read in data in xo like this:

if(fread(xo,23232,1, fp) != 1)  

Confused and discouraged and really looking for a helping voice!

Really thanking all in advance.

Rob

When extracting an integer scanf() stops when it

>when I get passed the 3 questions and want to read  

>I tried many variations of fread like: