Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8156837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:04:49+00:00 2026-06-06T17:04:49+00:00

I have a file with integers. I want to write in a buffer those

  • 0

I have a file with integers. I want to write in a buffer those integers as chars (its ascii number). Because it is part of a bigger project please do not post different but please help me on that. What I especially need is chars to be stored in a buffer of type char *.

These are my declarations.

FILE *in;
  long io_len = 1000;    
 char * buffer;
    in=fopen("input.txt","a+");

    buffer = malloc(io_len * sizeof(*buffer));
      if(buffer == NULL){
        perror("malloc");
        exit(EXIT_FAILURE);
        }

I am figuring out 2 sollutions.

If I write this one:

read_ret = read(in, buffer, io_len);

it reads from file in, io_len bytes and stores them in buffer. But it reads characters. So for example if I write 123 it will write to buffer 1,2,3 not the character with ascii number 123.

So I did this:

while((fscanf(in,"%d", &i))==1){
        printf(": %d\n", i);
    }

which reads the integers as I want. Now I am a little bit confused on how I will store them in buffer, as characters. I have tried this but it get me a segmentation fault.

while((fscanf(in,"%d", &i))==1){
        printf(": %d\n", i);
    buffer=(char) i;
    printf("Character in Buffer:%s\n",buffer);
    buffer++;
    }

Have in mind that later in my file I am writing my buffer somewhere else, so whatever I will do I want the pointer to be at the start of my char array(if it makes sense what I am saying)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:04:50+00:00Added an answer on June 6, 2026 at 5:04 pm

    Your final code should at least give you a warning about assigning an integer to a pointer in the line buffer=(char) i;. It looks like you want to dereference the pointer.

    You are also printing a string when it looks like you really only want to print a character at a time.

    Your code should probably look like this:

    int character_index = 0;
    while((fscanf(in,"%d", &i))==1){
            printf(": %d\n", i);
        buffer[character_index]=(char) i;
        printf("Character in Buffer:%c\n",buffer[character_index]);
        character_index++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to open a file, write some data on it so i have
I have this structure which I want to write to a file: typedef struct
I have an mXn matrix of integers. I want to write the matrix to
I have a 2D matrix myMatrix of integers which I want to save its
So I have a multipart form that has fields for text, integers and file
I have a *.txt files having integers, one on each line. So the file
i have to write in a file 4bytes representing an integer in little endian
I have file in json format of world-countries I want to change it to
I have the following script which identifies lines in a file which I want
I'm using Linq to write XML to a file and I want only sections

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.