Hi I need some help with debugging my program: It should read from the Console, process the input and give it back out:
The error occures after while(scanf("%15s", input) != EOF) is called the 2nd time. Unfortunately I can't tell you what the error is, because the progam freezes and doesn't give me any Information. I think there is something wrong with the input var (it is passed multiple times)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char* repeat(char c, int n);
char* drawLabel(char* label, int n);
char* drawBarnorm(char* label, int value);
char* drawBar(char* label, double value);
int main(void)
{
char* input;
double numIn;
char buf[] = "";
char* pOutput = &buf[0];
while(scanf("%15s", input) != EOF)
{
scanf("%lf", &numIn);
if (numIn > 1)
{
if (numIn > 30)
{
printf("num to big!\n");
return 0;
}
strcat(pOutput, drawBarnorm(input, (int)numIn));
} else
{strcat(pOutput, drawBar(input, numIn));}
printf("%s\n", pOutput);
}
printf("%s\n", pOutput);
return 0;
}
char* repeat(char c, int n)
{
char* out = (char*)malloc(sizeof(char)*50);
int i, len;
out[0] = '\0';
for (i = 0; i < n; ++i)
{
len = strlen(out);
out[len] = c;
out[len+1] = '\0';
}
return out;
}
char* drawLabel(char* label, int n)
{
if (strlen(label) > n)
{
char* newLabel = (char*)malloc(sizeof(char)*(n+1));
newLabel[0] = '\0';
strncpy(newLabel, label, n);
newLabel[n] = '\0';
return newLabel;
} else if (strlen(label) < n)
{
strcat(label, repeat(' ', n-strlen(label)));
}
return label;
}
char* drawBarnorm(char* label, int value)
{
char* bar = (char*)malloc(sizeof(char)*41);
char* barPart;
bar[0] = '\0';
bar = drawLabel(label, 8);
strcat(bar, "|");
barPart = drawLabel(repeat('#', value), 30);
strcat(bar, barPart);
strcat(bar, "|");
return bar;
}
char* drawBar(char* label, double value)
{
int val = (int)(30.0*value);
return drawBarnorm(label, val);
}
Thank you for helping me out with this.
char* input = malloc(size); /* Allocate memory of your wish */
Allocate memory to input You have not initialized your pointer.
The pointer should be pointing to some valid memory location to store the value through scanf()
You have to initialize input or declare it as an array like this
char input[16];
also, you should notice that scanf does not return EOF it returns the number of arguments matched, so you have to change
while(scanf("%15s", input) != EOF)
to
while(scanf("%15s", input) == 1)
because while(scanf("%15s", input) != EOF is always true.
Related
I am having trouble with a function that should read a string from the user. I am always getting (null) as the output.
Is this even a "right" approach for that kind of problem?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getString(char *input);
int main(void)
{
char *arr = NULL;
printf("please enter string: ");
getString(arr);
printf("%s", arr);
return 0;
}
int getString(char *input)
{
int i;
char c;
char *tmp;
input = malloc(sizeof(char));
for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
tmp = realloc(input, (i + 2) * sizeof(char));
if (tmp == NULL) {
free(input);
printf("allocation error");
return -1;
}
input = tmp;
input[i] = c;
}
input[i] = '\0';
return 0;
}
If you want to dynamically allocate the string you need to pass a pointer to char*, not just a char *. This way, the function can modify the real char * pointer and the caller will see the result. In your current code, the input variable only exists inside the function and does not affect the variable used by the caller, therefore your arr stays unchanged (NULL).
Something like this:
int getString(char **input)
{
int i;
char c;
char *tmp, *cur = NULL;
// No initial malloc() needed here.
// Let realloc() do the job passing NULL the first time.
for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
tmp = realloc(cur, (i + 2) * sizeof(char));
if (tmp == NULL) {
free(cur);
printf("allocation error");
return -1;
}
cur = tmp;
cur[i] = c;
}
cur[i] = '\0';
*input = cur;
return 0;
}
And then pass the parameter like this:
getString(&arr);
You should return the input pointer, since it is local to your function and get deallocated when the program leaves from the function, so in main the arr is still NULL.
int* getString(char *input);
int main(void)
{
//...
arr = getString(arr);
//...
}
int* getString(char *input)
{
//...
return input;
}
I am supposed to write a program to extract Web addresses starting with www. and ending with .edu. The program displays Web address contained in the input entered by the user. If the input does not contain a web address that starts with www. and ends with .edu, the program should display a message that indicates such a web address cannot be found.
Input: http://www.usf.edu/admission
Output: www.usf.edu
Input: https://www.facebook.com/
Output: Web address starting with www. and ending with .edu not found
However when my program runs, it is not displaying the correct output. I don't have any compiler errors or warnings so I'm not sure where the issue could be.
// This program extracts the text from the website URL
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STR_LEN 1000
void read_line(char *str, int n);
void pass_check(char *str);
void extract(char *s1, char *s2);
int main(void)
{
char instr[STR_LEN + 1];
char outstr[STR_LEN + 1];
printf("Please enter a URL: ");
read_line(instr, STR_LEN);
extract(instr, outstr);
puts(outstr);
pass_check(outstr);
return 0;
}
void extract(char *s1, char *s2) {
char *p, *q;
q = s2;
for (p = s1 + 7; *p != 0; p++) {
if (*p == '/')
break;
else {
*q = *p;
q++;
}
}
*q = '\0';
*p = '\0';
}
void read_line(char *str, int n) {
int ch;
int i = 0;
while ((ch = getchar()) != '\n') {
if (i < n) {
*str++ = ch;
i++;
}
}
*str = '\0';
}
void pass_check(char *str) {
const char *fref = "www";
const char *lref = "edu";
int len = strlen(str);
printf("%d", len);
char *l = &str[len - 3];
char f[STR_LEN + 1];
strncpy(f, str, 3);
if ((strcmp(f, fref) == 0) && strcmp(l, lref) == 0) {
printf("Output: ");
puts(str);
printf("\n");
} else
printf("Please only insert a .edu URL.");
}
The function strncpy() does not do what you think it does: strncpy(f, str, 3); will not append a null byte to f, so strcmp(f, fref); will actually have undefined behavior as f is uninitialized beyond the first 3 bytes.
Do not use this function, learn why from these blogs:
https://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/
https://blog.liw.fi/posts/strncpy/
Also note that your readline() function will run an infinite loop is the file is empty or not terminated by a newline.
Here is a corrected version:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define STR_LEN 1000
void read_line(char *str, size_t n);
int extract(const char *str, char *dest);
int main(void) {
char instr[STR_LEN + 1];
char outstr[STR_LEN + 1];
printf("Please enter a URL: ");
read_line(instr, sizeof(instr));
if (extract(instr, outstr)) {
puts(outstr);
} else {
printf("Web address starting with www. and ending with .edu not found\n");
}
return 0;
}
int read_line(char *str, size size) {
int ch;
size_t i = 0;
while ((ch = getchar()) != EOF && c != '\n') {
if (i + 1 < size) {
str[i++] = ch;
}
}
str[i] = '\0';
return (ch == EOF && i == 0) ? EOF : i;
}
int extact(const char *str, char *dest) {
const char *p;
*dest = '\0';
for (;;) {
if ((p = strstr(str, "https://www.")) != NULL) {
p += 8; // skip the https:// prefix
} else
if ((p = strstr(str, "http://www.")) != NULL) {
p += 7; // skip the http:// prefix
} else {
break;
}
// URL starts with www.
size_t len = strcspn(p, "/ \n"); // compute length of website name
if (len > 8 && !memcmp(p + len - 4, ".edu", 4)) {
// copy website name, assuming dest is at least as large as str
strncat(dest, p, len);
return 1;
}
str = p + len;
}
return 0;
}
I'm trying to parse through some CSV log files extracting only the n'th field (dismissing the others for speed). My function works as expected when I use a buffer size with fread greater than the size of the input.
The problem is when I read in part of the input and try to continue where I left off the next time the function is called. I believe the problem lies in how I'm handling the null terminator and setting my globals, but I just can't seem to figure it out.
Any help with understanding what I'm doing wrong greatly appreciated!
Code
#include <stdio.h>
#include <time.h>
int gcomc = 0;
int gpos = 0;
void test(char *str, int len)
{
const char *ptr = str;
char ch;
int i;
char so[10];
int comc = gcomc;
int pos = gpos;
for(i = 0; i < len; i++)
{
ch = ptr[i];
switch(ch)
{
case ';':
comc++;
break;
case '\0':
gcomc = comc;
gpos = pos;
break;
default:
if (comc == 3) {
ch = ptr[i];
so[pos++] = ch;
}
if (comc == 7) {
printf(" %s ", so);
comc = 0;
pos = 0;
gcomc = 0;
gpos = 0;
}
}
}
return;
}
int main(int argc, char* argv[])
{
FILE *fin=fopen("test.txt", "rb");
char buffer[100 + 1];
size_t bsz;
while((bsz = fread(buffer, sizeof *buffer, 100, fin)) > 0)
{
buffer[bsz] = '\0';
test(buffer, bsz);
}
return 1;
}
Input
A;B;C;D;E;F;G;H
I;J;K;L;M;N;O;P
Q;R;S;T;U;V;W;X
Y;Z;1;2;3;4;5;6
Output with buffer size of 100 (101)
D L T 2
Output with buffer size of 10 (11)
D P
Q X
Segmentation fault (core dumped)
Edit:
Thank you for the comments and code, I've reworked my (rather dumb written) code - any further criticism is welcome (constructive or destructive, I learn from it all):
#include <stdio.h>
#include <time.h>
void test(char *str, int len);
int gcomc, gpos = 0;
void test(char *str, int len)
{
const char *ptr = str;
char ch;
int i;
static char so[10];
for(i = 0; i < len; i++)
{
ch = ptr[i];
switch(ch)
{
case ';':
gcomc++;
break;
default:
if (gcomc == 3) {
ch = ptr[i];
so[gpos++] = ch;
}
if (gcomc == 7) {
so[gpos] = '\0'; /* ensure so is null terminated */
printf(" %s ", so);
gcomc = 0;
gpos = 0;
}
}
}
return;
}
extern int main()
{
FILE *fin=fopen("test.txt", "rb");
char buffer[10 + 1];
size_t bsz;
while((bsz = fread(buffer, sizeof *buffer, sizeof buffer, fin)) > 0)
{
test(buffer, bsz);
}
return 1;
}
There are at least two problems in your code to be able to read the file in chunks.
First, the so array is automatic: it has no reason to keep its values from one call to the others. You should declare it global (outside the test function) or static.
Next, you only copy the local state to global one when you find a null. But the null is at position len, and you exit the loop just before (for(i = 0; i < len; i++) note the <) so on next call you start again with 0, 0. You should choose one method to indicate the end of the buffer, either passing a length, of writing a null marker, but mixing both is error prone. As you use fread, my advice is to stick to a length:
In main use:
while((bsz = fread(buffer, sizeof *buffer, sizeof buffer, fin)) > 0)
{
test(buffer, bsz);
}
(that way, you only write the size of the buffer once)
and in test:
void test(char *str, int len)
{
const char *ptr = str;
char ch;
int i;
static char so[10];
int comc = gcomc;
int pos = gpos;
for(i = 0; i < len; i++)
{
ch = ptr[i];
switch(ch)
{
case ';':
comc++;
break;
default:
if (comc == 3) {
ch = ptr[i];
so[pos++] = ch;
}
if (comc == 7) {
so[pos] = '\0'; /* ensure so is null terminated */
printf(" %s ", so);
comc = 0;
pos = 0;
gcomc = 0;
gpos = 0;
}
}
}
gcomc = comc; /* store the state to globals */
gpos = pos;
return;
}
But as you were said in comments mixing local and globals like that is error prone. It looks like you started coding before designing the structure of the program and identifying what actually needed to be global. You didn't, did you? ;-)
The state of the parser inside test() need to survive the multiple call. You took care of this partly only make the counters global. Globals are bad practise. Also you miss to save the state (its content) of so.
Encapsulate the state in a structure.
#include <stdlib.h>
#include <stdio.h>
#define SO_SIZE (10)
struct state
{
size_t comc;
size_t pos;
char so[SO_SIZE + 1]; /* Add 1 for the 0-terminator. */
}
and pass it to each call of the parser (test() here).
Adjust the parser like this:
int test(struct state * pstate, const char *str, size_t len)
{
int result = 0; /* be optimistic. */
char ch;
size_t i;
for (i = 0; i <= len; i++)
{
ch = str[i];
switch (ch)
{
case ';':
pstate->comc++;
break;
default:
if (pstate->comc == 3)
{
ch = str[i];
if (SO_SIZE <= pstate->pos)
{
result = -1; /* overflow */
break;
}
pstate->so[pstate->pos++] = ch;
}
if (pstate->comc == 7)
{
printf(" %s ", pstate->so);
pstate->comc = 0;
pstate->pos = 0;
}
}
}
return result;
}
Then call it like this:
#define BUFFER_SIZE (100)
int main(void)
{
FILE *fin = fopen("test.txt", "rb");
if (NULL == fin)
{
perror("fopen() failed");
return EXIT_FAILURE;
}
{
char buffer[BUFFER_SIZE + 1] = {0};
size_t bsz;
struct state state = {0};
int result;
while (0 < (bsz = fread(buffer, sizeof *buffer, sizeof buffer, fin))
&& (0 == result))
{
result = test(&state, buffer, bsz);
}
return result ?EXIT_FAILURE :EXIT_SUCCESS;
}
}
I'm trying to write a code that goes through a given string using a pointer to parse it.
The original code I wrote worked fine but it was... redundant so I tried making it into a function call to make it more concise. Here is what i have:
char inputArray[300];
char buffer[300];
char username[100];
char password[100];
char name[100];
int i=0;
void repeat(char *to)
{
while(*to!='=')
{
to++;
}
}
void array(char *mm,char *tt)
{
i=0;
while(*tt!='+')
{
mm[i]=*tt;
tt++;
i++;
}
}
int main()
{
printf("give me the shit in this fashion: username=?+password=?+real=?\n");
scanf("%s",inputArray);
strcpy(buffer,inputArray);
char *tok=buffer;
repeat(tok);
tok++;
array(username,tok);
repeat(tok);
tok++;
array(password,tok);
tok++;
repeat(tok);
tok++;
array(name,tok);
}
For some reason it won't give me back the pointer array tok where it left off from the previous function call. why is that? it acts as if after calling it the pointer starts back from the beginning.
Functions receive copies of their arguments. Original arguments remain unaffected.
Giving something back has a special syntax in C: the return statement. Thus
char* repeat (char *to) // <- this function gives back a char*
{
while (*to != '=')
{
to++;
}
return to; // <- giving something back
}
Call it like this:
tok = repeat(tok);
Treat array in the same fashion.
Note 1, this function will result in *undefined behaviour if the string doesn't contain '='.
Note 2, it is also possible to pass a pointer to tok as the other answer suggests, but for sake of clarity it is only recommended to use this style when you need to return more than one thing from a function.
just change your repeat to this:
void repeat(char **to) {
while (**to != '=') {
(*to)++;
}
}
and call it like this:
repeat(&tok);
and always check for errors:
if (scanf("%299s", inputArray) != 1){
printf("incorrect input\n");
return 1;
}
and your sample code (and add check for errors in array and repeat to not go out of bounds):
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
char inputArray[300];
char buffer[300];
char username[300];
char password[300];
char name[300];
int i = 0;
void repeat(char **to) {
while (**to != '=') {
(*to)++;
}
}
void array(char *mm, char *tt){
i = 0;
while (*tt != '+') {
mm[i] = *tt;
tt++;
i++;
}
}
int main() {
printf("give me the shit in this fashion: username=?+password=?+real=?\n");
if (scanf("%299s", inputArray) != 1){
printf("incorrect input\n");
return 1;
}
inputArray[299] = 0;
strcpy(buffer, inputArray);
char *tok = buffer;
repeat(&tok);
tok++;
array(username, tok);
repeat(&tok);
tok++;
array(password, tok);
tok++;
repeat(&tok);
tok++;
array(name, tok);
}
and you may use this to not go out of bounds:
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
char* read_str(char *src, char *dst){
char *p, *q;
p = src;
while (*p != 0 && *p != '=') p++;
if (*p == 0) {
*dst = 0;
return NULL; // '=' not found
}
p++;
q = p;
while (*q != 0 && *q != '+') q++;
//if (*q == 0) return NULL;// '+' not found
while (p <= q) *dst++ = *p++;
dst--;
*dst = 0;
q++;
return q;
}
#define MAX_LEN 100
int main() {
char username[MAX_LEN];
char password[MAX_LEN];
char name[MAX_LEN];
char inputArray[MAX_LEN] = "username=Alex+password=123+real=Alex";
char *p = inputArray;
p = read_str(p, username);
if (p == NULL)return 1; // error
p = read_str(p, password);
if (p == NULL)return 1; // error
read_str(p, name);
printf("username: %s \n", username);
printf("password: %s \n", password);
printf(" name: %s \n", name);
}
I'm new to C. I'm having some trouble understanding some fundamental materials in reading input and pointers. I want to use a nextChar() function to read and print each character of a string that I enter in the command line. I try typing "hello"..It displays "hello" 6 times. Can someone tell me why this happens? How can I fix it? Thank you for your time!
#include <stdio.h>
#include <assert.h>
char nextChar(char* ptr)
{
static int i = -1;
char c;
++i;
c = *(s+i);
if ( c == '\0' )
return '\0';
else
return c;
}
void display(char* ptr)
{
assert(ptr != 0);
do
{
printf("%s", ptr);
} while (nextChar(ptr));
}
int main(int argc, const char * argv[])
{
char* ptr=argv[1];
display(ptr);
return 0;
}
The %s format specifier instructs printf to print an array of chars, until it finds a null terminator. You should use %c instead if you want to print a single char. If you do this, you'll also need to use the return value from nextChar.
Alternatively, more simply, you could change display to iterate over the characters in your string directly
void display(char* ptr)
{
assert(ptr != 0);
do
{
printf("%c", *ptr); // print a single char
ptr++; // advance ptr by a single char
} while (*ptr != '\0');
}
Or, equivalently but with less obvious pointer arithmetic
void display(char* ptr)
{
int index = 0;
assert(ptr != 0);
do
{
printf("%c", ptr[index]);
index++;
} while (ptr[index] != '\0');
}
the nextchar function could be reduced:
char nextChar(char* ptr)
{
static int i = 0;
i++;
return (*(ptr+i));
}
and display to
void display(char* ptr)
{
assert(ptr != 0);
char c = *ptr;
do
{
printf("%c", c);
} while (c = nextChar(ptr));
}
#include <stdio.h>
#include <assert.h>
char nextChar(const char* ptr){
static int i = 0;
char c;
c = ptr[i++];
if ( c == '\0' ){
i = 0;
}
return c;
}
void display(const char* ptr){
char c;
assert(ptr != 0);
while(c=nextChar(ptr)){
printf("%c", c);
}
}
int main(int argc, const char * argv[]){
const char* ptr=argv[1];
display(ptr);
return 0;
}