As the title suggests, I don't know where my mistake is with binary.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char binary[360];
void printstringasbinary(char* s)
{
// A small 9 characters buffer we use to perform the conversion
char output[9];
while (*s)
{
// Convert the first character of the string to binary using itoa.
// Characters in c are just 8 bit integers, at least, in noawdays computers.
itoa(*s, output, 2);
strcat(binary, output);
++s;
}
}
int main(int argc, char *argv[])
{
int i, n;
//printf("argc: %i\n", argc);
if (argc != 3)
{
puts("Converter [Text] [hex|dec|bin]");
return 0;
}
FILE *f = fopen("Output.txt", "a");
for (i = 0; i < strlen(argv[0]); i++)
{
if (strcmp(argv[2], "hex") == 0)
fprintf(f, "%X ", (unsigned int) argv[1][i]);
else if (strcmp(argv[2], "dec") == 0)
fprintf(f, "%i ", (unsigned int) argv[1][i]);
else if (strcmp(argv[2], "bin") == 0)
for (n = 0; n < strlen(argv[1]); n++)
{
printstringasbinary(argv[1][i]);
}
fprintf(f, "%s ", binary);
}
fclose(f);
return 0;
}
You iterate through the characters of argv[1][i], whereas i can take on values up to the string length of argv[0]. Depending on whether argv[0] is "longer" than argv[1], this can lead to a seg fault.
Did you mean printstringasbinary(argv[1][n]) instead of printstringasbinary(argv[1][i])? BTW: why - as pointed out by LPs - do you iterate through argv[1] at all?
for (i = 0; i < strlen(argv[0]); i++)
....
for (n = 0; n < strlen(argv[1]); n++)
{
printstringasbinary(argv[1][n]);
}
Related
Sort command of linux must sort the lines of a text file and transfer the output to another file. But my code gives a runtime error. Please rectify the pointer mistakes so that output.
In which line exactly should I make changes? Because there is no output after all.
I'm pasting the whole code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sortfile(char **arr, int linecount) {
int i, j;
char t[500];
for (i = 1; i < linecount; i++) {
for (j = 1; j < linecount; j++) {
if (strcmp(arr[j - 1], arr[j]) > 0) {
strcpy(t, arr[j - 1]);
strcpy(arr[j - 1], arr[j]);
strcpy(arr[j], t);
}
}
}
}
int main() {
FILE *fileIN, *fileOUT;
fileIN = fopen("test1.txt", "r");
unsigned long int linecount = 0;
int c;
if (fileIN == NULL) {
fclose(fileIN);
return 0;
}
while ((c = fgetc(fileIN)) != EOF) {
if (c == '\n')
linecount++;
}
printf("line count=%d", linecount);
char *arr[linecount];
char singleline[500];
int i = 0;
while (fgets(singleline, 500, fileIN) != NULL) {
arr[i] = (char*)malloc(500);
strcpy(arr[i], singleline);
i++;
}
sortfile(arr, linecount);
for (i = 0; i < linecount; i++) {
printf("%s\n", arr[i]);
}
fileOUT = fopen("out.txt", "w");
if (!fileOUT) {
exit(-1);
}
for (i = 0; i < linecount; i++) {
fprintf(fileOUT, "%s", arr[i]);
}
fclose(fileIN);
fclose(fileOUT);
}
The problem in your code is you do not rewind the input stream after reading it the first time to count the number of newlines. You should add rewind(fileIN); before the next loop.
Note however that there are other problems in this code:
the number of newline characters may be less than the number of successful calls to fgets(): lines longer than 499 bytes will be silently broken in multiple chunks, causing more items to be read by fgets() than newlines. Also the last line might not end with a newline. Just count the number of successful calls to fgets().
You allocate 500 bytes for each line, which is potentially very wasteful. Use strdup() to allocate only the necessary size.
Swapping the lines in the sort routine should be done by swapping the pointers, not copying the contents.
allocating arr with malloc is safer and more portable than defining it as a variable sized array with char *arr[linecount];
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sortfile(char **arr, int linecount) {
for (;;) {
int swapped = 0;
for (int j = 1; j < linecount; j++) {
if (strcmp(arr[j - 1], arr[j]) > 0) {
char *t = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = t;
swapped = 1;
}
}
if (swapped == 0)
break;
}
}
int main() {
FILE *fileIN, *fileOUT;
char singleline[500];
int i, linecount;
fileIN = fopen("test1.txt", "r");
if (fileIN == NULL) {
fprintf(stderr, "cannot open %s\n", "test1.txt");
return 1;
}
linecount = 0;
while (fgets(singleline, 500, fileIN)) {
linecount++;
}
printf("line count=%d\n", linecount);
char **arr = malloc(sizeof(*arr) * linecount);
if (arr == NULL) {
fprintf(stderr, "memory allocation failure\n");
return 1;
}
rewind(fileIN);
for (i = 0; i < linecount && fgets(singleline, 500, fileIN) != NULL; i++) {
arr[i] = strdup(singleline);
if (arr[i] == NULL) {
fprintf(stderr, "memory allocation failure\n");
return 1;
}
}
fclose(fileIN);
if (i != linecount) {
fprintf(stderr, "line count mismatch: i=%d, lilnecount=%d\n",
i, linecount);
linecount = i;
}
sortfile(arr, linecount);
for (i = 0; i < linecount; i++) {
printf("%s", arr[i]);
}
fileOUT = fopen("out.txt", "w");
if (!fileOUT) {
fprintf(stderr, "cannot open %s\n", "out.txt");
return 1;
}
for (i = 0; i < linecount; i++) {
fprintf(fileOUT, "%s", arr[i]);
}
fclose(fileOUT);
for (i = 0; i < linecount; i++) {
free(arr[i]);
}
free(arr);
return 0;
}
To get a different sort order, you would change the comparison function. Instead of strcmp() you could use this:
#include <ctype.h>
int my_strcmp(const char *s1, const char *s2) {
/* compare strings lexicographically but swap lower and uppercase letters */
unsigned char c, d;
while ((c = *s1++) == (d = *s2++)) {
if (c == '\0')
return 0; /* string are equal */
}
/* transpose case of c */
if (islower(c)) {
c = toupper(c);
} else {
c = tolower(c);
}
/* transpose case of d */
if (islower(d)) {
d = toupper(d);
} else {
d = tolower(d);
}
/* on ASCII systems, we should still have c != d */
/* return comparison result */
if (c <= d)
return -1;
} else {
return 1;
}
}
"*Program received signal SIGSEGV, Segmentation fault.
0x000000000040073b in concat (nextstr=0x7fffffffee66 "the",
longstr=0x7fffffffe750 "", i=0x0) at main.c:24
24 printf("%c -> %c\n", nextstr[j], longstr[*i]);
(gdb)"
// define max for command line
#define MAX_CHARS 1000
#include <stdio.h>
int concat(char[], char[], int *);
void printreverse(char[], int);
int main(int argc, char *argv[])
{
char longstring[MAX_CHARS] = { '\0' };
int i = 0, j;
if (argc < 2)
{
printf("%s requires command-line args\n", argv[0]);
return 1;
}
for (j = 1; j <= argc; j++)
{
if (concat(argv[j], longstring, i))
return 1;
}
i--;
longstring[i] = '\0'; //delete trailing space
printf("%s\n", longstring);
}
int concat(char nextstr[], char longstr[], int *i)
{
int j = 0;
while (nextstr[j] != '\0')
{
printf("%c -> %c\n", nextstr[j], longstr[*i]);
longstr[*i] = nextstr[j];
(*i)++;
j++;
if (*i > MAX_CHARS)
{
printf("Error: Input is too long!\n");
return 1;
}
}
if (j > 0)
{
if ((*i) + 2 > MAX_CHARS)
{
printf("Error: Input is too long!\n");
return 1;
}
longstr[*i] = ' ';
longstr[(*i) + 1] = '\0';
(*i)++;
}
return 0;
}
Pointer error most likely
"This takes a command line argument is checks the length but there's a seg fault"
I think the error is on line 33
but I can't figure out how to fix it
The main problem that is causing the crash is in your for loop:
for(j = 1; j <= argc; j++){
if(concat(argv[j], longstring, i)) return 1;
}
Since concat's third parameter is an int *, you need to pass a pointer to i. Also, you are iterating through the loop one too many times. If argc is 2, then the program name is in argv[0], and your argument is in argv[1]. So, change the loop to this:
for(j = 1; j < argc; j++) {
if (concat(argv[j], longstring, &i))
return 1;
}
So I want to write a program which would print out a text line that contains a certain word from a file. e.g. if I was looking for a word 'linux' it would print out
2 computers called linux00, linux01 and linux02. 5 manager,"
said linux00. "Hello linux00," said 7 here to see us?" said
linux01. "Well," said the 10 linux02. "You're all going to be
unplugged," said 12 goooooooooooo..." said linux00.
from a story.txt:
Once upon a time, there were three little computers called
linux00, linux01 and linux02. One day, the nice computer manager
came into the Linux Laboratory. "Hello nice computer manager,"
said linux00. "Hello linux00," said the nice computer manager.
"What brings you here to see us?" said linux01. "Well," said the
nice computer manager, "I've got bad news and I've got good
news." "What's the bad news?" said linux02. "You're all going to be
unplugged," said the nice computer manager. "What's the
goooooooooooo..." said linux00.
Here's my code:
#include<stdio.h>
#include <stdlib.h>
#define ARR_LEN 100
int getLine(FILE * fin,char a[],int n)
{
int find = contains("linux", 5, a, ARR_LEN);
int count;
int i;
i = 0;
char c = getc(fin);
while(c != '\n')
{
a[i] = c;
// printf ("%c", a[i]);
//i = 0;
if (a[i] == EOF){
return EOF;
}
if (find == 1)
{
printf("%c", c);
c = getc(fin);
}
i = i + 1;
}
if(a[i]=='\n')
{
if ((i - 1) > ARR_LEN) {
printf("warning msg: length is over array bounds\n");
}
// printf("length of line is: %d\n", i - 1);
//printf("%c", a[i]);
i = i + 1;
//printf("\n");
return i - 1;
}
}
int contains(char target[], int m, char source[], int n) {
int flag = 0; // the source originally does not contain the target
int i;
for(i = 0; i < n; i++) { // go through each character of the source string
int targetIndex = 0;
int j;
/*check if the preceding characters of the source string are a substring
that matches the target string*/
for(j = i; j < n && targetIndex < m; j++) {
if(target[targetIndex] == source[j]) {
targetIndex += 1;
if(targetIndex == m) { // the 'target' has been fully found
flag = 1;
break;
}
}
else
{
break;
}
}
if(flag == 1) // 'target' is already found, no need to search further
{
break;
}
}
return flag;
}
main(int argc,char ** argv)
{
setbuf(stdout,NULL);
char a[ARR_LEN];
FILE * fin;
if(argc<2){
printf("wrong number of arguments\n");
exit(0);
}
fin = fopen(argv[1], "r");
if (fin == NULL) {
printf("Cannot open %s\n", fin);
exit(0);
}
int t = 0;
int j = 0;
int find = contains("linux", 5, a, ARR_LEN);
while (j != EOF)
{
t = t + 1;
printf("%d ", t);
j = getLine(fin,a,ARR_LEN);
printf("\n");
}
fclose(fin);
}
The getLine function is alright and it prints out a text with a line number in front all good. But the problem is with this
if (find == 1)
{
printf("%c", c);
c = getc(fin);
}
part, where I want the program to only print out the line if "contains" finds a match in that line.
Thanks for any help & sorry for a long post!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char sentence[500];
char word[10] = "linux";
FILE* fp1 = fopen("strstr.txt","r");
if(fp1 == NULL)
{
printf("Failed to open file\n");
return 1;
}
while((fscanf(fp1,"%[^\n]\n",sentence)>0))
{
if(strstr(sentence,word)!=NULL)
printf("%s\n\n",sentence);
}
}
So I have a program that takes a file and reads in character by character and prints the character and the hexadecimal equivalent.
`
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *labFile;
char buf;
labFile = fopen("lab1.dat", "r");
if (labFile == NULL) perror("Error opening file\n");
while( (buf = fgetc(labFile) ) != EOF){
if(("%x", buf)< 16){
printf("%c 0%x\n", buf, buf);
}
else
printf("%c %x\n", buf, buf);
}
fclose(labFile);
return 0;
}
`
The program works the way I need it to except for one thing. I need the program to output the hex number on top then the character directly underneath the number and this process needs to continue horizontally.
Any help would be greatly appreciated!
You should output the characters as hex first, and save each read in character that has been printed until you run out of columns on your screen. You can then move to the next line, and print out the characters that were saved underneath the hex output.
You can simplify your logic to format your hex output into a single print statement.
When printing out the character, you need to have a plan to represent non-printable characters. In the sample program below, we handle it by printing two consecutive dots.
void print_chars (unsigned char *p, int num) {
int i;
for (i = 0; i < num; ++i) {
printf("%s%c%s",
isprint(p[i]) ? " " : ".",
isprint(p[i]) ? p[i] : '.',
(i < num-1) ? " " : "\n");
}
}
int main() {
FILE *labFile;
char buf;
int count = 0;
int num = COLUMNS/3;
char printed[num];
labFile = fopen("lab1.dat", "r");
if (labFile == NULL) perror("Error opening file\n");
while( (buf = fgetc(labFile) ) != EOF) {
printf("%s%02x", count ? " " : "", buf);
printed[count++] = buf;
if (count == num) {
count = 0;
putchar('\n');
print_chars(printed, num);
}
}
fclose(labFile);
if (count) {
putchar('\n');
print_chars(printed, count);
}
return 0;
}
The number of columns is divided by 3 since each character takes about 3 columns for output (2 hex characters, and a space). Retrieving the number of columns is system dependent, but you can just plug in 80 if you wish.
You could do something like this. ASSUMES your lines are < 100 chars long. Not safe.
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *labFile;
char buf[100];
labFile = fopen("lab1.dat", "r");
if (labFile == NULL) perror("Error opening file\n");
while(fgets(buf, 100, labFile)) {
if(rindex(buf, '\n')) *rindex(buf, '\n') = 0;
int i, n = strlen(buf);
for(i = 0; i != n; i++) {
printf("0%x ", buf[i]);
}
printf("\n");
for(i = 0; i != n; i++) {
printf("%c ", buf[i]);
}
printf("\n");
}
fclose(labFile);
return 0;
}
I have a problem with an "add calculator".
Valgrind reports no memory errors, no errors from compiler but the program doesn't show any output despite the printf - "Base is ".
All pointers, and variables are (n my opinion) correctly initialized.
getnum function gets a number, returns a pointer to char - char *,
add function processes two numbers as strings, returns result which is a pointer to char (char *) as well.
I don't know whether the problem is memory allocation or procedures connected to processing arrays...
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX(A,B) ((A)>(B) ? (A) : (B))
char *getnum(FILE *infile, int base)
{
int len = 10;
int c;
int pos = 0;
char *num = NULL;
char *tmpnum = NULL;
num = malloc(sizeof(char)*len);
while (((c = fgetc(infile)) != EOF) && (isalnum(c))) {
if (isdigit(c)) {
/* irrelevant*/
}
else if (isalpha(c)) {
fprintf(stderr, "Wrong base, expected 16\n");
free(num);
return NULL;
}
if (pos >= len) {
/*realloc*/
}
}
return num;
}
int main(int argc, char **argv)
{
FILE *infile = NULL;
char *number1 = NULL;
char *number2 = NULL;
char *result = NULL;
int base, i, j = 0, length, count = 0;
infile = fopen(argv[1], "r");
base = atoi(argv[2]);
while (!feof(infile)) {
number1 = getnum(infile, base);
number2 = getnum(infile, base);
break;
}
printf("Base is %d\n", base);
result = add(number1, number2, base);
length = strlen(result);
for (i = 0; i <= length - 1; i++) {
if (result[i] == '0') {
count++;
}
}
for (j = i; j == (length - 1); j++) {
printf("Result is: %s\n", &result[j]);
break;
}
free(result);
result = NULL;
fclose(infile);
return 0;
}
Trying to work it out for the past 4 hours and can't find a mistake.
Thanks in advance!
There is one severe typo near the end of main().
for (j = i; j == (length - 1); j++) {
/* ^^ SHOULD BE <= */
printf("Result is: %s\n", &result[j]);
break;
}
Looking at this code:
for (i = 0; i <= length - 1; i++) {
if (result[i] == '0') {
count++;
}
}
if (count == length) {
printf("Result is 0\n");
free(result);
result = NULL; /* arguable */
fclose(infile);
return 0;
}
for (i = 0; i <= length - 1; i++) {
if (result[i] != '0') {
break;
}
}
for (j = i; j == (length - 1); j++) {
printf("Result is: %s\n", &result[j]);
break;
}
Instead of counting the total number of zeroes in the output number, and then counting the number of leading zeroes again, why not combine the two?
What is the last loop about? It's not even really a loop - it will execute once if i is length - 1, or not at all if not (presumably you're hitting the latter case in your test input).
e.g.
for (count = 0; count < length; count++) {
if (result[count] != '0')
break;
}
if (count == length) {
printf("Result is 0\n");
free(result);
result = NULL; /* arguable */
fclose(infile);
return 0;
}
printf("Result is: %s\n", &result[count]);