Reading a string without wasting memory - c

I have to read a string from stdin, allocating memory dinamically without wasting it.
I've done this, but i'm not convinced about it,because in this way i think i waste memory!
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *alloc_memory(int n)
{
char *p;
p=malloc(n*sizeof(char));
if(p==NULL)
{
fprintf(stderr,"Error in malloc\n");
exit(EXIT_FAILURE);
}
return p;
}
int main(int argc, char *argv[])
{
if(argc != 1)
{
fprintf(stderr,"Usage: %s \n",argv[0]);
return EXIT_FAILURE;
}
char string[64];
int lung;
char *p,*s,*w;
printf("Insert string: \n");
p=fgets(string,63,stdin);
if(p==NULL)
{
fprintf(stderr,"Error in fgets\n");
exit(EXIT_FAILURE);
}
printf("You've inserted: %s", string);
lung=strlen(p);
s = alloc_memory(lung+1);
w=strncpy(s,p,lung);
printf("Final string:%s", w);
return EXIT_SUCCESS;
}
any idea? Should i read one character at a time?

To have char str[64] (string is not a good name for a variable, it might lead to ambiguities) declared only temporarily just put it in a local context:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char * alloc_memory(size_t n)
{
char * p = malloc(n); /* * sizeof(char) is always 1 */
if (p == NULL)
{
fprintf(stderr, "Error in malloc() when trying to allocate %zu bytes.\n", n);
exit(EXIT_FAILURE);
}
memset(p, 0, n); /* Avoid having strncpy() choke .. later down in this example. */
return p;
}
int main(int argc, char * argv[])
{
if (argc != 1)
{
fprintf(stderr, "Usage: %s \n", argv[0]);
return EXIT_FAILURE;
}
{
char * w = NULL;
printf("Insert string: ");
{
char str[64]; /* here str is allocated */
char * p = fgets(str, 63, stdin);
if (p == NULL)
{
fprintf(stderr, "Error in fgets().\n");
exit(EXIT_FAILURE);
}
printf("You've inserted: '%s'\n", str);
{
size_t lung = strlen(p);
char * s = alloc_memory(lung + 1);
w = strncpy(s, p, lung);
}
} /* here "str" is deallocated */
printf("Final string: '%s'\n", w);
}
return EXIT_SUCCESS;
}

Related

Cannot use both Argv's, cannot run Move twice segmentation fault

Ive been trying various ways to get my program to work. regardless of weather i try argv1 or argv2 first, the second one will segmentation fault. even if i try to print SOURCE2DEFINE or argv[2] AFTER a move() it will segmentation fault. i cannot move both files trying to run move twice will result in a segmentation fault. im assuming that it has to be something to do with pointers and allocation.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#define SOURCEDEFINE argv[1]
#define SOURCE2DEFINE argv[2]
#define DESTDEFINE argv[argc - 1]
#define ARGCDEFINE argc
#define COMMANDDEFINE argv[0]
int getIndex(char, char*);
char* fileGetName(char*);
void move(char*, char*);
void copy(char*, char*);
int main(int argc, char** argv)
{
printf("Test Argc: %d\n", argc);
int lengthArray = argc-2;
printf("Test Length: %d\n", lengthArray);
printf(" command %s\n", COMMANDDEFINE);
printf("%s\n", DESTDEFINE);
if(strcmp("./copy", COMMANDDEFINE) == 0)
{
// copy(source, dest);
}
else if(strcmp("./move", COMMANDDEFINE) == 0)
{
int i = 1;
printf("Test 1: %s\n", argv[i]);
printf("Test 2: %s\n", argv[argc-1]);
move(SOURCEDEFINE, DESTDEFINE);
printf("%s Filename debug \n", SOURCE2DEFINE);
move(SOURCE2DEFINE, DESTDEFINE);
// i++;
}
return 0;
}
void moveMultiple(int argc, char** argv){
int index = 1;
while(argv[index] != NULL){
if(index < argc - 1){
move(argv[index],argv[argc - 1]);
index++;
}
}
}
void move(char *source, char* dest)
{
printf("Running Move\n");
// FILE *s = fopen(source, "r");
// FILE *s;
//FILE *s = fopen(source, "r");
strcat(dest, fileGetName(source));
int l = link(source, dest);
//if(s == NULL)
if(l)
{
printf("Error, File Not Found");
perror("Link");
fflush(stdout);
exit(1);
}
remove(source);
}
void copy(char *source, char* dest)
{
printf("Running Copy\n");
strcat(dest, fileGetName(source));
int l = link(source, dest);
//if(s == NULL)
if(l)
{
printf("Error, File Not Found");
perror("Link");
fflush(stdout);
exit(1);
}
}
char* fileGetName(char *filename)
{
int i = 0;
int length = strlen(filename);
char *catString;
int index = getIndex('/', filename);
index--;
memcpy(catString,&filename[index], length);
return catString;
}
int getIndex(char i, char *s)
{
printf("Running getIndex\n");
int index = -1;
for(int l =0; l<strlen(s); l++){
if(s[l] == i) {
index = l;
}
}
return index;
}
Your move method changes dest (which is really argv[i]), and overwrites the memory after it: strcat(dest, fileGetName(source));. This destroys the other parameter and probably some other things. Don't write strings into memory you don't own.

Read a file using "read()" function

i'm studying C, and I need to read a text-file, but I can only use "write, malloc, free, open, read, close".
That's my code:
#define MAXCHAR 10000
int open_fp(int check)
{
char *int_vector;
int fp,len;
int i,j;
char buffer[MAXCHAR];
if(check == 0) //standard list
{
if((fp = open("file.txt", O_RDONLY)) != -1) //check if the fp is opened. -1 = error
{
printf("\n%d\n",fp); // DEBUG FUNCTION
sleep(1);
if (!(int_vector = (char*)malloc(sizeof(char) * sizeof(char))))
{
printf("\nWrong allocation\n!"); // DEBUG FUNCTION
return(0);
}
len = read(fp,int_vector,MAXCHAR);
for(i=0;i<len;i++)
{
printf("%c",int_vector[i]);
}
}
else
{
printf("File error!");
return (0);
}
}
return(0);
}
Now my question is: As you can read here,
char buffer[MAXCHAR];
i've created static buffer, but i would like create a dynamic buffer which allow me to resize the buffer according to the number of the chars in the text file, but i don't know how... someone have a trick😥😥 ?
First of all your way of allocating memory is wrong in below line.
//This allocates only 2 bytes of memory, but you are trying to read 10000
if (!(int_vector = (char*)malloc(sizeof(char) * sizeof(char))))
correct that line as below
//better to add one byte extra and store \0 at the end, useful in case of string operations
if (!(int_vector = malloc(MAXCHAR+1)))
and as far as your question is concerned, you dont need to reallocate memory in this particular case because you are just reading the bytes to buffer and printing.
a single malloc will suffice.
#include<stdlib.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAXCHAR 100
int open_fp(int check)
{
char *int_vector;
int fp,len;
int i,j;
char buffer[MAXCHAR];
if(check == 0) //standard list
{
if((fp = open("file.txt", O_RDONLY)) != -1) //check if the fp is opened. -1 = error
{
printf("\n%d\n",fp); // DEBUG FUNCTION
sleep(1);
if (!(int_vector = (char*)malloc(MAXCHAR)))
{
printf("\nWrong allocation\n!"); // DEBUG FUNCTION
return(0);
}
//not doing memset on purpose because only limited bytes are accessed.
while(len = read(fp,int_vector,MAXCHAR))
{
printf("\n **number of bytes read is %d **\n",len);
for(i=0;i<len;i++)
{
printf("%c",int_vector[i]);
}
}
printf(" At last LEN = %d\n", len);
//free the memory at the end
free(int_vector);
int_vector = NULL;
close(fp);// better to as fd
}
else
{
printf("File error!\n");
return (0);
}
}
return(0);
}
int main()
{
open_fp(0);
return 0;
}
ehm, If you forget to set realloc to it as well, here is some sample code for reallocation (dynamic resizing buffer)
#include <stdio.h>
#include <stdlib.h>
int main () {
char *str;
/* Initial memory allocation */
str = (char *) malloc(15);
strcpy(str, "tutorialspoint");
printf("String = %s, Address = %u\n", str, str);
/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %u\n", str, str);
free(str);
return(0);
}

reading file`s lines char by char into char** array

I wrote the next function that tries to read and enter each line from text file into a string array in c :
int main(int argc,char* argv[])
{
char ** lines;
readFile(argv[1],lines);
}
int readFile(char* filePath,char** lines)
{
char file_char;
int letter_in_line=0;
int line=1;
char* line_string=malloc(1024);
int j=1;
int fd=open(filePath,O_RDONLY);
if (fd < 0)
{
return 0;
}
while (read(fd,&file_char,1) >0)
{
if(file_char != '\n' && file_char != '0x0')
{
line_string[letter_in_line] = file_char;
letter_in_line++;
}
else
{
if(lines != NULL)
{
lines=(char**)realloc(lines,sizeof(char*)*line);
}
else
{
lines=(char**)malloc(sizeof(char*));
}
char* line_s_copy=strdup(line_string);
lines[line-1]=line_s_copy;
line++;
letter_in_line=0;
memset(line_string,0,strlen(line_string));
}
j++;
}
printf("cell 0 : %s",lines[0]);
return 1;
}
I have 2 questions :
1)Whenever the code reaches the print of cell 0, I'm getting
Segmentation fault (core dumped) error. What is wrong ?
2)In case I
want to see the changes in the lines array in my main, I should pass
&lines to the func and get char*** lines as an argument ? In
addition, I will need to replace every 'line' keyword with '*line' ?
*I know that I can use fopen,fget, etc... I decided to implement it in this way for a reason.
There is many issues that make your code core dump.
Here a version very similar to your code. I hope it will help you to understand this.
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
int read_file(const char *filename, char ***result)
{
/* open the file */
const int fd = open(filename, O_RDONLY);
if (fd < 0) {
*result = NULL;
return -1;
}
/* read the file characters by characters */
char *buffer = (char *)malloc(sizeof(char) * 1024);
char c;
int column = 0;
int line = 0;
*result = NULL;
/* for each characters in the file */
while (read(fd, &c, 1) > 0) {
/* check for end of line */
if (c != '\n' && c != 0 && column < 1024 - 1)
buffer[column++] = c;
else {
/* string are null terminated in C */
buffer[column] = 0;
column = 0;
/* alloc memory for this line in result */
*result = (char **)realloc(*result, sizeof(char *) *
(line + 1));
/* duplicate buffer and store it in result */
(*result)[line++] = strdup(buffer);
}
}
free(buffer);
return line;
}
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s [filename]", argv[0]);
return 1;
}
char **lines;
int line_count = read_file(argv[1], &lines);
if (line_count < 0) {
fprintf(stderr, "cannot open file %s\n", argv[1]);
return 1;
}
for(int i=0; i < line_count; i++)
printf("%s\n", lines[i]);
return 0;
}
Here an other version:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int read_file(const char *filename, char ***result)
{
/* init result */
*result = NULL;
/* open the file */
FILE *file = fopen(filename, "r");
if (file == NULL)
return -1;
/* read the file line by line */
char *buffer = (char *)malloc(sizeof(char) * 1024);
int line = 0;
while (fgets(buffer, 1024, file)) {
*result = (char **)realloc(*result, sizeof(char *) *
(line + 1));
(*result)[line++] = strdup(buffer);
}
free(buffer);
return line;
}
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s [filename]", argv[0]);
return 1;
}
char **lines;
int line_count = read_file(argv[1], &lines);
if (line_count < 0) {
fprintf(stderr, "cannot open file %s\n", argv[1]);
return 1;
}
for(int i=0; i < line_count; i++)
printf("%s\n", lines[i]);
return 0;
}

In function 'print_usage': error: expected ')' before 'PROGRAM'

As the title states, I am receiving an error when I try to build my project which states: In function 'print_usage': error: expected ')' before 'PROGRAM'
I am not exactly sure what is causing this error and was hoping someone could point me in the right direction.
#include <stdio.h>
#include <stdlib.h>
#include "lexer.h"
void print_usage();
void load_file(char *filename, char **buffer, size_t *size);
int main(int argc, char *argv[]) {
argc--;
argv++;
if(argc <= 0) {
print_usage();
exit(EXIT_FAILURE);
}
while(argc > 0) {
size_t size; /* I'm not using this, but it could be useful. */
Token token;
char *buffer = NULL;
unsigned int token_cnt = 0;
load_file(*argv, &buffer, &size);
if(buffer) {
char *b; /* editable copy of the buffer pointer */
b = buffer;
while(get_token(&b, &token) != TOKEN_EOF) {
printf("%s: ", id_string[token.id]);
if(token.str) {
printf("\"%s\"\n", token.str);
free(token.str);
}
else {
printf("no string found\n");
}
if(token.id != TOKEN_BAD) {
token_cnt++;
}
}
printf("\n----------------------\n");
printf("%u valid tokens found\n", token_cnt);
printf("----------------------\n");
free(buffer);
}
argc--;
argv++;
}
exit(EXIT_SUCCESS);
}
void print_usage() {
fprintf(stderr, "Usage: " PROGRAM " [file]...\n");
return;
}
argc--;
argv++;
/* Since you just incremented argv, you just lost the program name! */
/* You need argv[0] as the program name to create the error message */
if(argc <= 0) {
print_usage( argv[0] );
exit(EXIT_FAILURE);
}
void print_usage(char* program) {
fprintf(stderr, "Usage: %s [file]...\n", program);
}
Look up the syntax for fprintf(), you probably need something more like
void print_usage() {
fprintf(stderr, "Usage: %s [file]...\n", PROGRAM);
return;
}
Assuming PROGRAM is defined as a string somewhere.

Reading lines from file

I am trying to read strings and integers from a simple text file to my array. But the problem is that I get some random characters in a line in the middle of my list. It probably has to do with a newline problem, but I am not sure. The text file looks like this:
4
Mr Tambourine Man
Bob Dylan
1965
Dead Ringer for Love
Meat Loaf
1981
Euphoria
Loreen
2012
Love Me Now
John Legend
2016
The first number (4), indicates how many songs there are in the list. I have made a struct which will be able to hold the songs and dynamically allocate memory for each pointer.
Struct:
typedef struct Song {
char *song;
char *artist;
int *year;
} Song;
Allocated:
Song *arr;
arr = (Song*)malloc(sizeof(Song));
Function:
int loadFile(char fileName[], Song *arr, int nrOf) {
FILE *input = fopen(fileName, "r");
if (input == NULL) {
printf("Error, the file could not load!\n");
} else {
int i = 0;
fscanf(input, "%d\n", &nrOf);
for (int i = 0; i < nrOf; i++) {
arr[i].song = (char*)malloc(sizeof(char));
arr[i].artist = (char*)malloc(sizeof(char));
arr[i].year = (int*)malloc(sizeof(int));
fgets(arr[i].song, 100, input);
fgets(arr[i].artist, 100, input);
fscanf(input, "%d\n", arr[i].year);
}
printf("The file is now ready.\n");
fclose(input);
}
return nrOf;
}
Are you able to find the problem? Or do you have a better solution?
This is wrong:
arr[i].song = (char*)malloc(sizeof(char));
arr[i].artist = (char*)malloc(sizeof(char));
You are only allocating buffers of size 1, there's no scaling. This gives you undefined behavior when you overrun the buffers by loading more data into them than they can hold.
I would expect those to read:
arr[i].song = malloc(100);
and so on. Note that no cast is necessary, and sizeof (char) is always 1.
Also, this:
arr[i].year = (int*)malloc(sizeof(int));
is super-strange. There's absolutely no reason to dynamically allocate a single integer, just make the field an int and store the value there directly.
First Issue:
arr[i].song = (char*)malloc(sizeof(char));
arr[i].artist = (char*)malloc(sizeof(char));
Are only allocating 1 byte for your char* pointers, song and artist. You can allocate a size for this:
arr[i].song = (char*)malloc(100 * sizeof(char)); /* or malloc(100) */
arr[i].artist = (char*)malloc(100 * sizeof(char));
Or you can simply malloc() enough space from you buffer:
char buffer[100];
fgets(buffer, 100, input);
/* check for failure, remove newline */
arr[i].song = malloc(strlen(buffer)+1);
/* check error from malloc */
strcpy(arr[i].song, buffer);
Or even use strdup():
arr[i].song = strdup(buffer);
Which is a substitute for malloc()/strcpy().
Note: You can also read Do I cast the result of malloc?.
Second Issue:
Your current struct:
typedef struct Song {
char *song;
char *artist;
int *year;
} Song;
Can be simplified to:
typedef struct {
char *song;
char *artist;
int year;
} Song;
Because year does not need to be a pointer. Easier to manage if its just an int. This avoids having to do allocations like:
arr[i].year = (int*)malloc(sizeof(int));
Other Recommendations:
You should check the return of fscanf() and fgets() as its safe to do this. It helps just incase your file will have incorrect data. This goes the same for malloc(), which can return NULL is unsuccessfully allocated on the heap.
Here is some code with the above considerations in mind:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
typedef struct {
char *song;
char *artist;
int year;
} Song;
Song *create_array(FILE *input, int *nrof);
void load_data(Song *arr, FILE *input, int nrof);
void print_free_data(Song *arr, int nrof);
void get_buffer(char buffer[], FILE *input);
int main(void) {
FILE *input;
Song *arr;
int nrof;
input = fopen("artist.txt", "r");
if (input == NULL) {
fprintf(stderr, "Error reading file\n");
exit(EXIT_FAILURE);
}
arr = create_array(input, &nrof);
load_data(arr, input, nrof);
print_free_data(arr, nrof);
fclose(input);
return 0;
}
Song *create_array(FILE *input, int *nrof) {
Song *arr;
if (fscanf(input, "%d ", nrof) != 1) {
fprintf(stderr, "Cannot find number of songs\n");
exit(EXIT_FAILURE);
}
arr = malloc(*nrof * sizeof(*arr));
if (arr == NULL) {
fprintf(stderr, "Cannot allocate %d spaces for array\n", *nrof);
exit(EXIT_FAILURE);
}
return arr;
}
void load_data(Song *arr, FILE *input, int nrof) {
char buffer[SIZE];
for (int i = 0; i < nrof; i++) {
get_buffer(buffer, input);
arr[i].song = malloc(strlen(buffer)+1);
if (arr[i].song == NULL) {
fprintf(stderr, "Cannot allocate song\n");
exit(EXIT_FAILURE);
}
strcpy(arr[i].song, buffer);
get_buffer(buffer, input);
arr[i].artist = malloc(strlen(buffer)+1);
if (arr[i].artist == NULL) {
fprintf(stderr, "Cannot allocate artist\n");
exit(EXIT_FAILURE);
}
strcpy(arr[i].artist, buffer);
if (fscanf(input, "%d ", &arr[i].year) != 1) {
fprintf(stderr, "Cannot find year for Song: %s Album: %s\n",
arr[i].song, arr[i].artist);
exit(EXIT_FAILURE);
}
}
}
void get_buffer(char buffer[], FILE *input) {
size_t slen;
if (fgets(buffer, SIZE, input) == NULL) {
fprintf(stderr, "Error from fgets(), line not read\n");
exit(EXIT_FAILURE);
}
slen = strlen(buffer);
if (slen > 0 && buffer[slen-1] == '\n') {
buffer[slen-1] = '\0';
} else {
fprintf(stderr, "Too many characters entered\n");
exit(EXIT_FAILURE);
}
}
void print_free_data(Song *arr, int nrof) {
for (int i = 0; i < nrof; i++) {
printf("%s\n%s\n%d\n\n", arr[i].song, arr[i].artist, arr[i].year);
free(arr[i].song);
arr[i].song = NULL;
free(arr[i].artist);
arr[i].artist = NULL;
}
free(arr);
arr = NULL;
}
Which Outputs correct data:
Mr Tambourine Man
Bob Dylan
1965
Dead Ringer for Love
Meat Loaf
1981
Euphoria
Loreen
2012
Love Me Now
John Legend
2016
Your memory allocation is incorrect. The structure should have char arrays for the song and artist names and an int for the year, and you should modify your API to return the array and its size to the caller:
int loadFile(const char *fileName, Song **arr, int *numberp);
Here is a corrected and simplified of your program:
#include <stdio.h>
#include <stdlib.h>
typedef struct Song {
char song[100];
char artist[100];
int year;
} Song;
/* call as
if (loadFile(fileName, &songs, &songs_size) < 0) {
// deal with error...
}
*/
int loadFile(const char *fileName, Song **arrp, int *numberp) {
FILE *input;
Song *arr;
int i, nrOf;
input = fopen(fileName, "r");
if (input == NULL) {
fprintf(stderr, "Cannot open file %s\n", filename);
return -1;
} else {
if (fscanf(input, "%d\n", &nrOf) != 1) {
fprintf(stderr, "%s: missing number of items\n", filename);
fclose(intput);
return -1;
}
arr = calloc(sizeof(*arr), nrOf);
if (arr == NULL) {
fprintf(stderr, "cannot allocate memory for %d items\n", nrOf);
fclose(intput);
return -1;
}
for (int i = 0; i < nrOf; i++) {
char cc;
if (fscanf(input, "%99[^\n]%*c%99[^\n]%*c%d%c",
sarr[i].song, arr[i].artist,
&arr[i].year, &cc) != 4 || cc != '\n') {
fprintf(stderr, "%s: invalid format for item %d\n",
filename, i);
break;
}
}
printf("The file is now ready.\n");
fclose(input);
*arrp = arr;
*numberp = i;
return i;
}
}

Resources