Question

I never saw that before:

enter image description here

What is the bottom left corner above? The latest version of the program is

#include <stdio.h>
#include <stdlib.h> 
int main(int argc, char **argv)
{
   int ch;
   char file_name[25] = "/proc/scsi/scsi";
   FILE *fp; 
   fp = fopen(file_name,"r"); // read mode 
   if (fp == NULL)
   {
      perror(file_name);
      exit(EXIT_FAILURE);
   } 
   printf("The contents of %s file are :\n", file_name); 
   while ((ch = fgetc(fp)) != EOF)
      putchar(ch); 
   fclose(fp);
   return 0;
}

Test

$ cc driveinfo.c;./a.out 
The contents of /proc/scsi/scsi file are :
Attached devices:
Host: scsi0 Channel: 00 Id: 00 Lun: 00
  Vendor: ATA      Model: WDC WD2500JS-75N Rev: 10.0
  Type:   Direct-Access                    ANSI  SCSI revision: 05
Host: scsi1 Channel: 00 Id: 00 Lun: 00
  Vendor: ATA      Model: ST3250824AS      Rev: 3.AD
  Type:   Direct-Access                    ANSI  SCSI revision: 05
Host: scsi2 Channel: 00 Id: 00 Lun: 00
  Vendor: TSSTcorp Model: DVD+-RW TS-H653A Rev: D300
  Type:   CD-ROM                           ANSI  SCSI revision: 05
Host: scsi3 Channel: 00 Id: 00 Lun: 00
  Vendor: Optiarc  Model: DVD-ROM DDU1681S Rev: 102A
  Type:   CD-ROM                           ANSI  SCSI revision: 05
Host: scsi4 Channel: 00 Id: 00 Lun: 00
  Vendor: Lexar    Model: USB Flash Drive  Rev: 1100
  Type:   Direct-Access                    ANSI  SCSI revision: 00
Host: scsi5 Channel: 00 Id: 00 Lun: 00
  Vendor: WD       Model: 5000AAKB Externa Rev: l108
  Type:   Direct-Access                    ANSI  SCSI revision: 00

The following reproduces the strange output:

#include <stdio.h>
#include <stdlib.h> 
int main(int argc, char **argv)
{
   int ch;
   char file_name[25] = "/proc/scsi/scsi-notExist";
   FILE *fp; 
   fp = fopen(file_name,"r"); // read mode 
   if (fp == NULL)
   {
      perror(&file_name[25]);
      exit(EXIT_FAILURE);
   } 
   printf("The contents of %s file are :\n", file_name); 
   while ((ch = fgetc(fp)) != EOF)
      putchar(ch); 
   fclose(fp);
   return 0;
}

Update

The clang compiler will warn but not (g)cc:

$ clang -Wconversion cpu-disk-info.c
cpu-disk-info.c:14:15: warning: array index of '25' indexes past the end of an
      array (that contains 16 elements) [-Warray-bounds]
      perror(&file_name[25]);
              ^         ~~
cpu-disk-info.c:6:4: note: array 'file_name' declared here
   char file_name[] = "/proc/scsi/scsi";
   ^
1 warning generated.
dev@dev-OptiPlex-745:~$ gcc -Wconversion cpu-disk-info.c
dev@dev-OptiPlex-745:~$ 
Was it helpful?

Solution

It's most likely due to not passing a valid const char* to void perror(const char *s), which makes the file name in <filename>: No such file or directory output as garbage/non printable characters.

The perror() function shall map the error number accessed through the symbol errno to a language-dependent error message, which shall be written to the standard error stream as follows:

First (if s is not a null pointer and the character pointed to by s is not the null byte), the string pointed to by s followed by a colon and a .

Then an error message string followed by a .

OTHER TIPS

I think you correct the warning driveinfo.c:17 or post your code. And its OK ;)

Good luck !

"varning att skicka heltal utan konvertering ..." sounds like it's a call to perror with something that isn't a string, perhaps?

If you post the code, we can probably see more clearly, but I'm pretty sure that warning and the output is related.

Edit: If you pass filename[25] to the function, I expect this warning. If you pass &filename[25], then you are probably passing in something that isn't a string at all, so some random bytes gets printed, which could print almost anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top