Unable to read in file -- C - c

I'm sorry, I've done something similar to this before and I'm sure I'm over complicating this, but could someone help me understand whats wrong? I've only known java previously, but I'm getting familiar with C.
I have tried 3 different ways from searches online, the one not commented out is the one I'd prefer to use. It's printing out the right amount of numbers in data.txt but it only prints out the number 1. I thought the c = scanf inside the while would give me a different result each time.
I also tried just reading in the numbers as a char since I'm not doing any math, but I got a bunch of funky symbols.
input:
./a.out < data.txt
data.txt contents:
0 2 2
0 6 1
0 7 4
1 7 5
0 8 2
0 8 9
1 15 13
c file content:
#include
#include "queue.h"
int main(void)
{
/*
char c = scanf("%c", &c);
while (c!= EOF)
{
printf("%c", c);
c = scanf("%c", &c);
}//while
*/
int c = scanf("%d", &c);
while (c!= EOF)
{
printf("%d", c);
c = scanf("%d", &c);
}//while
printf("\n");
/*
char c;
//char **argv
FILE *infile;
infile=fopen(argv,"r");
while (!feof(infile))
{
fscanf(infile, "%c", &c);
printf("%c", c);
} // while
fclose(infile);
*/
return 0;
}

You should use a different variable for checking the result of scanf than for storing the value read. As you have it now, you immediately overwrite the read value with the scanf result.
Also, it is better to check for success than to check for EOF, as if there is text entered you will go into an infinite loop.
int x = scanf("%d", &c);
while ( x == 1 )
{
printf("%d", c);
x = scanf("%d", &c);
}
Obviously this can be condensed:
while ( 1 == scanf("%d", &c) )
printf("%d", c);

Related

What are the numbers associated with printing getchar()?

Code:
int main(void) {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar();
printf("%d", c);
}
}
I enter a character and that character is returned with putchar(c), however, if I print that character I get a code back? example:
0 = 48010
1 = 49110
2 = 50210
etc...
what are these numbers? is this where the character is stored in memory or something?
cheers
So I believe I figured out the problem by introducing newlines
int main(void) {
int c;
c = getchar();
while (c != EOF) {
putchar(c);
printf("\n");
c = getchar();
printf("%d\n", c);
}
}
When I enter 1 this returns the following in the terminal window.
49
1
10
What's happening is I'm returning the ASII character code for 1 - > 49
returning the value I entered with getchar()
and returning 10 which is the linefeed value i.e. the enter command.
Right? without the newlines it was just concatenating them all, making it confusing.

Using a while loop to iterate through user input until specific number

I am trying to print out each integer on a new line, given integers separated by whitespace from a user input. It stops printing after a specific number, lets say 84. For example
The user input is
20 -4 84 8
How could i use a while loop to print these out as
20
-4
84
i know about scanf("%d %d %d %d", a, b, c, d), However the input size would be unknown, such that there could be only 3 numbers or 7 numbers. So far i have:
#include <stdio.h>
int main(void)
{
int i = 0;
int x = 0;
scanf("%d", &x);
while (x != 84) {
print("%d\n", x[i]);
i++;
}
}
Push the scanf into the while condition. Something like
while (scanf("%d", &x) != EOF && x != 84)
print("%d\n", x);
The basic concept should be of two-steps:
Read the number
Check and decide whether to print /continue.
A psuedo-code would look like
while ((ret =scanf(x)) && ret != EOF ){
if (x == VAL) break;
printf(x);
}
You have a few errors:
the array num does not exist;
the scanf must be repeated inside the while loop.
The corrected code is thw following:
#include <stdio.h>
int main(void)
{
int x;
while( 1 ){
scanf( "%d", &x );
print( "%d\n", x );
if( x==84 ) break;
}
}
You need to put scanf() inside the while loop in order to update every new input. No need for arrays if you want to print only the inputted values because you can print it after new input. Better do it with do-while loop.
#include <stdio.h>
int main(void)
{
int i = 0;
int x = 0;
do {
if ( scanf("%d", &x) < 0 )
break;
printf("%d\n", x);
} while (x != 84);
}

Stop scanf loop if user enters a specific number (Not working) C

I've looked at multiple solutions but none of them worked for me.
I'm asking the user to enter numbers in a loop, but if the user enters a specific number the loop should break.
This is what I've got so far.
#include <stdio.h>
#include <stdlib.h>
#define MAXNUMBERS 5
int getNumbers(int array[])
{
int i;
int n = 0;
printf("Enter max. %d numbers, enter empty line to end:\n", MAXNUMBERS);
for (i = 0; i < MAXNUMBERS; i++)
{
scanf("%d", &array[i]);
fflush(stdin);
n++;
if (array[i] == '5')
{
break;
}
}
return n;
}
int main()
{
int array[MAXNUMBERS];
int amount_numbers;
amount_numbers = getNumbers(array);
printf("Numbers entered: %d\n", amount_numbers);
printf("First three: %d %d %d", array[0], array[1], array[2]);
return 0;
}
Input:
1
5
4
3
2
Output:
Numbers entered: 5
First three: 1 5 4
If the user enters 5 the loop should break.
I'm using 5 as an example, I later want it to do with an empty line. But it doesn't even work with 5.
It just keeps prompting the user to enter another number after he entered 5.
The actual problem is '5' != 5 the former is the character 5 which is in fact it's ascii value, and the latter is the number 5, since you are reading integers, i.e. using the "%d" specifier in scanf() you should use 5, but it would be better if it was just a int variable, and you could initialize it to any number you like before the loop starts.
Your loop is wrong anyway because if the user enters a non-numeric value then your program will invoke undefined behavior. Besides you already invoke undefined behavior with fflush(stdin), so
Remove fflush(stdin)1
7.21.5.2 The fflush function
If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is
undefined.
So the behavior is undefined for an input stream like stdin, or even if the most recent operation was input.
You must check that the value was read properly, and then check in the loop condition if it equals the value you want to stop the loop with, try this
int readNumber()
{
int value;
printf("input a number > ");
while (scanf("%d", &value) == 1)
{
int chr;
printf("\tinvalid input, try again...\n");
do { /* this, will do what you thought 'fflush' did */
chr = getchar();
} ((chr != EOF) && (chr != '\n'));
printf("input a number > ");
}
return value;
}
int getNumbers(int array[])
{
int i;
int stop = 5;
printf("Enter max. %d numbers, enter empty line to end:\n", MAXNUMBERS);
array[0] = 0;
for (i = 0 ; ((i < MAXNUMBERS) || (array[i] == stop)) ; i++)
array[i] = readNumber();
return i;
}
1This is a quote from the C11 draft 1570.
if (array[i] == '5')
You're checking whether array[i] is equal to the ASCII value of the character '5'.
Remove the '' to make it compare against the integer 5.
You are checking if an integer is equal to the character '5', which is then being cast to an ascii value of '5'.
Try using this:
if (array[i] == 5)
Disregard everything!
I should have written
if (array[i] == 5)
without the quotes!
I'm an idiot!
I sat 2 hours at this error...

C - scanf has gone ROGUE

I have this c program where I am inputing a number N followed by N more numbers. For example, I'll enter 100 followed by 100 more numbers. For some reason, after so many inputs the scanf function will stop working properly. It's as if it has stopped taking input and will just continue one with whatever value is in size.
The use case I came up with is 100 1 2 3 4 5 6 7 8 9 10... (repeated ten times). then after three or four times of that I'll type in 100 10 9 8 7 6 5 4 3 2 1... (repeated ten times) and then there will be an infinite loop of print statements.
int main(int argc, const char * argv[]) {
int histogram[10000];
int i;
while (1) {
int *rectPtr = histogram;
int size;
scanf("%d", &size);
if (!size) return 0;
for (i = 0; i < size; ++i) {
scanf("%d", rectPtr);
rectPtr++;
}
printf("%d", 1);
printf("\n");
}
return 0;
}
Distrust infinite loops.
In a series of comments, I said:
You're not testing the return value from scanf(), so you don't know whether it is working. The pair of printf() statements is odd; why not write printf("%d\n", 1); or even puts("1");?
Your code does not test or capture the return value from scanf(), so you do not know whether scanf() is reporting a problem. As a general rule, test the return value of input functions to make sure what you thought happened did in fact happen. You could also print out the values read just after you read them:
if (scanf("%d", rectPtr) != 1)
{
fprintf(stderr, "scanf() failed\n");
return 1;
}
printf("--> %d\n", *rectPtr);
rectPtr++;
Similarly when inputting size. Also consider if (size <= 0) return 0;. And using fgets() plus `sscanf() can make reporting errors easier.
j.will commented:
It is great to know if scanf fails, but I want to know why it fails and prevent it from failing. How do I do that?
I responded:
I understand you'd like to know. With scanf(), the best you can do after a failure is usually to read all the characters that follow up to a newline or EOF, and if you want to know what went wrong, then you print those characters too, because scanf() leaves the last character that it read in the input buffer ready for the next input operation.
void gobble(void)
{
printf("Error at: <<");
int c;
while ((c = getchar()) != EOF && c != '\n')
putchar(c);
puts(">>");
if (c == EOF)
puts("<<EOF>>");
}
The first character in the output is what caused the failure.
See also How to use sscanf() in loops?
Hacking your code to match this:
#include <stdio.h>
static void gobble(void)
{
printf("Error at: <<");
int c;
while ((c = getchar()) != EOF && c != '\n')
putchar(c);
puts(">>");
if (c == EOF)
puts("<<EOF>>");
}
int main(void)
{
enum { MAX_VALUES = 10000 };
int histogram[MAX_VALUES];
int size;
while (printf("Number of items: ") > 0 && scanf("%d", &size) == 1 &&
size > 0 && size <= MAX_VALUES)
{
int *rectPtr = histogram;
for (int i = 0; i < size; ++i)
{
if (scanf("%d", rectPtr) != 1)
{
gobble();
return 1;
}
rectPtr++;
}
printf("size %d items read\n", size);
}
return 0;
}
IMO, you need to check the return value of scanf() for proper operation. Please check the below code. I have added some modifications.
To exit from the program, you need to press CTRL+ D which will generate the EOF. Alternatively, upon entering some invalid input [like a char instead of int] wiil also cause the program to beak out of while() llop and terminate.
I have put the sequence to check first scanf(). All others need to be checked, too.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
int histogram[10000] = {0};
int i;
int *rectPtr = histogram;
int size = 0;
int retval = 0;
printf("Enter the number of elements \n");
while ( (retval = scanf("%d", &size)) != EOF && (retval == 1)) {
rectPtr = histogram;
if (!size) return 0;
printf("Enter %d elements\n", size);
for (i = 0; i < size; ++i) {
scanf("%d", rectPtr); //check in a simmilar way to above
rectPtr++;
}
printf("%d\n", 1111111);
printf("Enter the number of elements: \n");
}
return 0;
}
The output of a sample run
[sourav#broadsword temp]$ ./a.out
Enter the number of elements: 2
Enter 2 elements
1
2
1111111
Enter the number of elements: 3
Enter 3 elements
1
2
3
1111111
Enter the number of elements: 9
Enter 9 elements
0
9
8
7
6
5
4
3
2
1111111
Enter the number of elements: r
[sourav#broadsword temp]$
histogram is declared to have size 10000. You say you do 100 1 2 3 ... repeated 10 times. If I correctly understand that uses 1000 slots in histogram.
If you repeat the test more than 10 times, you exhaust histogram and begin to write past the end of array causing undefined behaviour.
So you must either :
reset recPtr = histogram at each iteration
control recPtr - histogram + size <= sizeof(histogram) after reading size (IMHO better)
And as other said, you should always control input operations : anything can happen outside of your program ...

How to auto append newline at the end of a file

I am newbie and have a C - Linux program that read a number of variable lines like this:
12 34 56 78
3 4 7 900
...
%d %d %d %d
...
1 2 3 4
The function I've written so far is like this:
void Import(const char* path_of_file)
{
FILE *cfile = fopen(path_of_file, "r");
int lines = 0;
//I copied it from another question
while (EOF != (fscanf(cfile,"%*[^/n]"), fscanf(cfile, "%*c"))) ++lines;
fclose(cfile);
cfile = fopen(path_of_file, "w");
...
for( i=0; i<n; i++)
{
...
fscanf(cfile, "%d %d %d %d", &a, &b, &c, &d);
...
}
...
//Of course it will return something, but it shouldn't matter here
fclose(cfile);
}
But may be because the text file is created in Windows or some thing, it doesn't always end with a newline, and so my program frequently miss the last line.
Is there anyway to automatic append a newline at the end of file if it's not there? Or better, is there any way to read the last line without that newline? A example function based on the one above will be very good.
Thank you Pavel, I have never read the other answer in that post, my fault for only read the Accepted answer. After read all those posts, I chosen to use this code to count the line, it can avoid the problem above:
int n = 0;
int pc = EOF;
int c;
while ((c = getc(cfile)) != EOF) {
if (c == '\n')
++n;
pc = c;
}
if (pc != EOF && pc != '\n')
++n;
Sorry for my stupidness.

Resources