0

I am trying to write a program that reads text from a file, converts the characters to uppercase and then writes the output on a new file. The code works just fine for the reading and converting to uppercase parts, but for the writing the output part when I create a char* for the name of the output file, I get a segmentation fault.

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

void lowerToUpper(char* temp)
{
    char* name;
    name = strtok(temp,":");

    // Convert to upper case
    char* s = name;
    while (*s)
    {
        *s = toupper((unsigned char) *s);
        s++;
    }
}


int main()
{
    int fd;
    char* file_name_read = "./test.txt";
    fd = open(file_name_read, O_RDONLY);
    char* buf_rd;

    ssize_t nr;
    size_t byte_count = 1000;
    off_t offset = 0;

    nr = pread(fd, buf_rd, byte_count, offset);

    close(fd);

    lowerToUpper(buf_rd);

    char* file_name_write = "./test_uppercase.txt";

    /* CODE FOR WRITING TO THE FILE */

    return 0;
}

When I remove the char* file_name_write line, the code works fine. When I include it in the code, I get a segmentation fault.

I have tried

  • removing the call to the lowerToUpper() inside main
  • using char file_name_write[] instead of char* file_name_write
  • using malloc() to allocate space and then assign its value
  • using different byte_count and offset values

Edit: The problem was an uninitialized pointer with buf_rd. When I added

char* buf_rd = (char*) malloc(1000 * sizeof(char));

it solved the problem.

Thank you Mr Lister and lurker!

5
  • 4
    Where do you initialise buf_rd? Commented Jul 1, 2019 at 13:22
  • @MrLister It gets initialized in pread(). I also tried initializing it with NULL and other temporary values but the problem still continues. Commented Jul 1, 2019 at 13:24
  • 1
    @OmerFY no it doesn't. pread expects you to pass a valid buffer pointer. buf_rd doesn't hold a valid buffer pointer. It's an uninitialized pointer. Initializing it with NULL will cause pread to attempt to write to address NULL, which is bad. Unclear what "other temporary values" means in your context. And you said you have tried using malloc but haven't shown exactly what/how you did that. Commented Jul 1, 2019 at 13:26
  • @lurker I tried using malloc for the file_name_write but now I have tried it with buf_rd and it solved the problem. Thank you! Commented Jul 1, 2019 at 13:31
  • You need something like buf_rd = malloc(byte_count * sizeof *buf_rd); before calling pread Commented Jul 1, 2019 at 13:32

2 Answers 2

3
char* buf_rd;
...
nr = pread(fd, buf_rd, byte_count, offset);

You did not allocate memory for buf_rd. It is just a pointer.

Sign up to request clarification or add additional context in comments.

1 Comment

I noticed that and I already updated the answer. Thanks, though ;) I wrote the answer before re-reading the docs for strtok().
1

You use buf_rd as buffer, but that variable is only declared and never initialized.

The documentation says:

ssize_t pread(int fd , void * buf , size_t count , off_t offset );

pread() reads up to count bytes from file descriptor fd at offset offset (from the start of the file) into the buffer starting at buf. The file offset is not changed.

It is expected of you to initialize the a buffer that can be used by the pread function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.