How to copy contents of system() command obtained text files in windows? - c

I'm trying to get the HDD serial number from the following code & also trying to copy the lines from the "HDDserial.txt" to another text file called "Bond.txt":
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char ch;
FILE *fp1,*fp2;
system("wmic path win32_physicalmedia get SerialNumber > HDDserial.txt");
fp1 = fopen("HDDserial.txt","r");
fp2 = fopen("Bond.txt","w");
while((ch= fgetc(fp1)!= EOF))
{
fputc(ch,fp2);
}
fputs("\nSender\n",fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
I'm getting output of "HDDserial.txt" like this:
SerialNumber
32534230394a5a44303333333931202020202020
but my "Bond.txt" gives me some peculiar characters.like this image:
But I want the same output as "HDDserial.txt" in "Bond.txt" !
Can anyone please help me with my codes?so that I can fix this problem & get done proper copying?

!= binds tighter than =.
So this
ch = fgetc(fp1) != EOF
assigns the result of fgetc(fp1) != EOF to ch, which is 1 until EOF had been reached, which makes it 0.
To correct this use parentheses like this:
(ch = fgetc(fp1)) != EOF
Also fgetc() returns int not char. So make ch an int.
int main()
{
int ch;
...
while ((ch = fgetc(fp1)) != EOF)
{
fputc((char) ch, fp2);
}
...
Or introduce an intermediate variable:
int main()
{
int result;
...
while ((result = fgetc(fp1)) != EOF)
{
char ch = (char) result;
fputc(ch, fp2);
}
...

Related

How to take first row from this list of text?

I have a list of columns containing text but I just to fetch first upper row from this list. How to do that?
#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;
}
Your stop condition is EOF, everything will be read to the end of the file, what you need is to read till newline character is found, furthermore EOF (-1) should be compared with int type.
You'll need something like:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fr;
int c;
if(!(fr = fopen("prog.txt", "r"))){ //check file opening
perror("File error");
return EXIT_FAILURE;
}
while ((c = fgetc(fr)) != EOF && c != '\n')
{
printf("%c",c); /* display on screen*/
}
fclose(fr);
return EXIT_SUCCESS;
}
This is respecting your code reading the line char by char, you also have the library functions that allow you to read whole line, like fgets() for a portable piece of code, or getline() if you are not on Windows, alternatively download a portable version, and, of course you can make your own like this one or this one.
For whatever it's worth, here's an example that uses getline
#include <stdio.h>
int main()
{
FILE *fr;
char *line = NULL;
size_t len = 0;
ssize_t nread;
if (!(fr = fopen("prog.txt", "r"))) {
perror("Unable to open file");
return 1;
}
nread = getline(&line, &len, fr);
printf("line: %s, nread: %ld\n", line, nread);
fclose(fr);
return 0;
}
Some notes:
getline() can automatically allocate your read buffer, if you wish.
getline() returns the end of line delimiter. You can always strip it off, if you don't want it.
It's ALWAYS a good idea to check the status of I/O calls like "fopen()".
just replace EOF as '\n'(new line char). Than your code will read until reaching the new line. Here is what it looks like:
#include <stdio.h>
int main()
{
FILE *fr;
char c = ' ';
fr = fopen("prog.txt", "r");
while(c != EOF && c != '\n')
{
c = fgetc(fr); /* read from file*/
if(c != EOF){
printf("%c",c); /* display on screen*/
}
}
fclose(fr);
return 0;
}
I have not tested it yet but probably work. Please let me know if there is some problem with the code i will edit it.
Edit1:char c; in line 5 is initialized as ' ' for dealing with UB.
Edit2:adding condition (c != EOF) to while loop in line 7, for not giving reason to infinite loop.
Edit3:adding if statement to line 10 for not printing EOF which can be reason for odd results.

How does following statement produce spaces?

What my program "upper" trying to do is making letters upper case. It gets a file from the commmand line as argv; then reads it afterwards it makes them uppercase.
An example: "i wonder if it works" in the example.txt file. In command line:
C:\Users\...>upper example.txt
I WONDER IF IT WORKS
This was the code I used first:
int main (int argc, char *argv[]){
FILE * fp;
int ch;
if ((fp = fopen (argv[1] , "r+")) == NULL) {
fprintf (stderr , "Can not be opened.");
exit(EXIT_FAILURE);
}
while((ch = getc(fp)) != EOF){
if (isalpha(ch))
putchar(toupper(ch));
else
putchar(' ');
}
fclose(fp);
return 0;
}
It works but I saw a more concise version which doesn't need the else statement.
while((ch = getc(fp)) != EOF){
putchar(toupper(ch));
}
And it puts the spaces between each word too. How is this possible?
From the documentation
int toupper(int c);
Converts c to its uppercase equivalent if c is a lowercase letter and has an uppercase equivalent. If no such conversion is possible, the value returned is c unchanged.

the function "fgetc ()" does not work properly in a While

I wrote a little program that is supposed to read the contents of a file character by character but what the code does is that it jumps each time a character as if it escapes a caracters each time and I don't understand why
and i dont know what to do
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, age = 18;
char strind[] = "Himou";
FILE *file = NULL;
file = fopen("test.txt", "r+");
if(file != NULL)
{
do
{
printf("%c", fgetc(file));
}while(fgetc(file) != EOF);
fclose(file);
}
else
{
printf("the file couldn't be open");
}
return 0;
}
the file exists and contains "Hello World !!Your name is Himou and your age is 18", so thats what i expected but the actual result "HloWrd!Yu aei io n oraei 8"
it jumps each time a character as if it escapes a caracters each time
do
{
printf("%c", fgetc(file)); <<< here you read and print a character
}while(fgetc(file) != EOF); << here you read again a character and lost it
yes this is true, because you ask for that, see comments I added in the code above
i dont know what to do
probably you want something like that to write all the read characters :
int c;
while ((c = fgetc(file)) != EOF)
putchar(c);

Opening File byte by byte printing in hex

C programmer newcomer here.
I'm trying to open a .obj file (containing LC3 instructions) and print them in groups of 2 bytes line by line in hex. I've tried opening the file and iterating through char by char and printing in hex but I am unsure how to group the bytes together in groups of 2 to print them together. I am also printing out a group of "fffffff"s for the bytes that lead with a 1 (I assume).
void readFile(const char *fileName) {
FILE *file;
file = fopen(fileName, "rb");
char ch;
while ((ch = fgetc(file)) != EOF) {
if (isprint(ch)) {
printf("%x", ch);
}
else {
printf("%02x", ch);
if (ch == '\n') {
fputs("\n", stdout);
}
}
}
fclose(file);
}
The output I am looking to achieve is:
0x4500
0x2009
0xe209
0xa409
But I am getting:
0x45
0020
09fffffffe209fffffffa40956
I understand that the hex is printing the excess "ffffffff"s due to not being an unsigned char but I am struggling to print close to the desired output. Any help in printing in 2 byte groups or how to remove the "fffffff"s would be greatly appreciated, and I'm really struggling.
The getchar()
family of functions (including getc() and fgetc()) all behave similarly; they return an int, not a char. Read the values into an int and live life happy.
void readFile(const char *fileName)
{
FILE *file = fopen(fileName, "rb");
if (file == 0)
return;
int ch;
while ((ch = fgetc(file)) != EOF)
{
printf("0x%.2x", ch);
if ((ch = fgetc(file)) == EOF)
break;
printf("%.2x\n", ch);
}
putchar('\n');
fclose(file);
}
If there's an even number of bytes in the file, you'll get an extra newline at the end. If that's a problem, keep a record of whether you exit the loop at the top (no newline needed) or from the middle (newline needed).
Both isprint(ch) and ch == '\n' have absolutely nothing to do with 2-byte grouping.
Perhaps you want something simple like this:
unsigned char ch;
while ((ch = (unsigned char)fgetc(file)) != EOF) {
printf("0x%02x", ch);
if ((ch = (unsigned char)fgetc(file)) != EOF)
printf("%02x", ch);
printf("\n");
}

Sideways conCATenate

Hi I have recently been given a task in C.
The aim of the task is to read from two text files and output each line of each file side by side with a separator string in the middle of said lines.
Example:
file 1 contains:
green
blue
red
file 2 contains:
rain
sun
separator string = xx
output =
greenxxrain
bluexxsun
redxx
I have managed to do this but was wondering if anyone else has any alternatives. Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int f1, f2;
FILE *file1, *file2;
file1 = fopen("textone", "r"); //open file1 for reading.
file2 = fopen("texttwo", "r"); //open file2 for reading.
//if there are two files ready, proceed.
if (file1 && file2){
do{
//read file1 until end of line or end of file is reached.
while ((f1 = getc(file1)) != '\n' && f1!= EOF ){
//write character.
putchar(f1);
}
//print separator string.
printf("xx");
//read file2 until end of line or end of file is reached.
while ((f2 = getc(file2)) != '\n' && f2!= EOF ){
//write character.
putchar(f2);
}
putchar('\n');
//do this until both files have reached their end.
}while(f1 != EOF || f2 != EOF);
}
}
You might find fgets(3) useful. It can be used to read a whole line at once. That said, it has downsides, too - you need to know how long the line is going to be, for example, or at least handle the case where the line is longer than your buffer. Your implementation seems fine to me (except that you should call fclose(3)).
You could write a simple function to avoid the 'big' repetition in the do { ... } while loop:
static void read_and_echo_line(FILE *fp)
{
int c;
while ((c = getc(fp)) != EOF && c != '\n')
putchar(c);
}
...
do
{
read_and_echo_line(file1);
printf("xx");
read_and_echo_line(file2);
putchar('\n');
} while (!feof(file1) || !feof(file2));
In this context, it is fairly reasonable to use feof() as shown (though it is not a function to use most of the time). Alternatively:
static int read_and_echo_line(FILE *fp)
{
int c;
while ((c = getc(fp)) != EOF && c != '\n')
putchar(c);
return(c);
}
...
do
{
f1 = read_and_echo_line(file1);
printf("xx");
f2 = read_and_echo_line(file2);
putchar('\n');
} while (f1 != EOF || f2 != EOF);

Resources