0

These are the instructions: "Read characters from standard input until EOF (the end-of-file mark) is read. Do not prompt the user to enter text - just read data as soon as the program starts."

So the user will be entering characters, but I dont know how many. I will later need to use them to build a table that displays the ASCII code of each value entered.

How should I go about this?

This is my idea

int main(void){
     int inputlist[], i = -1;
     do {++i;scanf("%f",&inputlist[i]);}
         while(inputlist[i] != EOF)
user3321551
  • 123
  • 2
  • 3
  • 15
  • 1
    Couple of mistakes. You are trying to read a float into an integer variable. Also you are writing into the inputlist array without allocating memory for it. – user376507 Feb 18 '14 at 03:20
  • It is better to change the title because the problem not in scanf(). The problem is in memory management. – likern Feb 18 '14 at 05:28

4 Answers4

0

You said character.So this might be used

char arr[10000];
ch=getchar();
while(ch!=EOF)
{
   arr[i++]=ch;
   ch=getchar();
}
//arr[i]=0; TO make it a string,if necessary.

And to convert to ASCII

for(j=0;j<i;j++)
     printf("%d\n",arr[j]);

If you are particular in using integer array,Use

int arr[1000];
while(scanf("%d",&arr[i++])!=EOF);

PPS:This works only if your input is one character per line.

scanf returns EOF on EOF

rjv
  • 6,058
  • 5
  • 27
  • 49
0

You have a reasonable attempt at a start to the solution, with a few errors. You can't define an array without specifying a size, so int inputlist[] shouldn't even compile. Your scanf() specifier is %f for float, which is wrong twice (once because you declared inputlist with an integer type, and twice because you said your input is characters, so you should be telling scanf() to use %c or %s), and really if you're reading input unconditionally until EOF, you should use an unconditional input function, such as fgets() or fread(). (or read(), if you prefer).

You'll need two things: A place to store the current chunk of input, and a place to store the input that you've already read in. Since the input functions I mentioned above expect you to specify the input buffer, you can allocate that with a simple declaration.

char input[1024];

However, for the place to store all input, you'll want something dynamically allocated. The simplest solution is to simply malloc() a chunk of storage, keep track of how large it is, and realloc() it if and when necessary.

char *all_input;
int poolsize=16384;
all_input = malloc(pool_size);

Then, just loop on your input function until the return value indicates that you've hit EOF, and on each iteration of the loop, append the input data to the end of your storage area, increment a counter by the size of the input data, and check whether you're getting too close to the size of your input storage area. (And if you are, then use realloc() to grow your storage.)

This isn't my real name
  • 4,869
  • 3
  • 17
  • 30
0

You could read the input by getchar until reach EOF. And you don't know the size of input, you should use dynamic size buffer in heap.

char *buf = NULL;
long size  = 1024;
long count = 0;
char r;

buf = (char *)malloc(size);
if (buf == NULL) {
    fprintf(stderr, "malloc failed\n");
    exit(1);
}   

while( (r = getchar()) != EOF) {
    buf[count++] = r;
    // leave one space for '\0' to terminate the string 
    if (count == size - 1) {
        buf = realloc(buf,size*2);
        if (buf == NULL) {
            fprintf(stderr, "realloc failed\n");
            exit(1);
        }   
        size = size * 2;
    }   
}   
buf[count] = '\0';
printf("%s \n", buf);

return 0;
Kang Li
  • 606
  • 7
  • 10
  • In C, do not cast the value returned by the `malloc()` function. – This isn't my real name Feb 18 '14 at 03:37
  • Cool thanks! One thing I'm confused about is, how does the user enter in a EOF value to stop the getchar() function from requesting input? – user3321551 Feb 19 '14 at 03:49
  • @user3321551 "Ctrl" + "D" is stand for EOF in linux. Which you can refer more detail in http://stackoverflow.com/questions/3197025/end-of-fileeof-of-standard-input-stream-stdin – Kang Li Feb 19 '14 at 06:09
0

Here is full solution for your needs with comments.

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

// Number of elements
#define CHARNUM 3

int main(int argc, char **argv) {
    // Allocate memory for storing input data
    // We calculate requested amount of bytes by the formula:
    // NumElement * SizeOfOneElement
    size_t size = CHARNUM * sizeof(int);

    // Call function to allocate memory
    int *buffer = (int *) calloc(1, size);

    // Check that calloc() returned valid pointer
    // It can: 1. Return pointer in success or NULL in faulire
    //         2. Return pointer or NULL if size is 0 
    //            (implementation dependened). 
    //            We can't use this pointer later.

    if (!buffer || !size)
    {
        exit(EXIT_FAILURE);
    }

    int curr_char;
    int count = 0;
    while ((curr_char = getchar()) != EOF)
    {
        if (count >= size/sizeof(int))
        {
            // If we put more characters than now our buffer
            // can hold, we allocate more memory
            fprintf(stderr, "Reallocate memory buffer\n");
            size_t tmp_size = size + (CHARNUM * sizeof(int));
            int *tmp_buffer = (int *) realloc(buffer, tmp_size);
            if (!tmp_buffer)
            {
                fprintf(stderr, "Can't allocate enough memory\n");
                exit(EXIT_FAILURE);
            }
            size = tmp_size;
            buffer = tmp_buffer;
        }
        buffer[count] = curr_char;
        ++count;
    }


    // Here you get buffer with the characters from 
    // the standard input
    fprintf(stderr, "\nNow buffer contains characters:\n");
    for (int k = 0; k < count; ++k)
    {
        fprintf(stderr, "%c", buffer[k]);
    }
    fprintf(stderr, "\n");

    // Todo something with the data

    // Free all resources before exist
    free(buffer);
    exit(EXIT_SUCCESS); }

Compile with -std=c99 option if you use gcc.

Also you can use getline() function which will read from standard input line by line. It will allocate enough memory to store line. Just call it until End-Of-File.

errno = 0; 
int read = 0; 
char *buffer = NULL;
size_t len = 0;
while ((read = getline(&buffer, &len, stdin)) != -1) 
{ // Process line } 

if (errno) { // Get error }

// Process later

Note that if you are using getline() you should anyway use dynamic allocated memory. But not for storing characters, rather to store pointers to the strings.

likern
  • 3,744
  • 5
  • 36
  • 47