C :: How To Use Fgets
Sep 4, 2013
Currently I am doing the first exercises from Illustrating C. The exercise that I am trying first is the one where someone can input degrees and the program will be able to put those in to sin or cos. Im trying to use fgets to take input from the user. the answer can only be sin or cos. Im having trouble with how to get it to work.
My goal is to have the output of the choice sin or cos. Store that choice. proceed to ask what the degrees are from the user. then i would have the degree input multiplied by pi/180 converting it to radians and having the program compute it that way
Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
int a;
int main()
}
[code]....
View 11 Replies
ADVERTISEMENT
Dec 11, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COL 70
#define MAX_ROW 20
}
[code]...
With my input.txt file being Code: abcd efgh And in particular, there is no new line after the letter h, but when I print out the text string, I get a new line after h. Why is this?
View 5 Replies
View Related
Aug 31, 2014
I am trying to write a terminal-like chat application in Linux. I would like to use a FIFO queue to print out the messages in terminal. The queue would be populated from 2 sources- stdin and messages sent from the other user over TCP. I have meet an obstacle that I cannot handle...
Lets say I would like to take user input using fgets and put it into a buffer. Then queue it if the buffer is not empty or print if it is. The problem is that when I use fgets or scanf, my input is instantly printed to the terminal..If i do:
Code:
fgets(message, 100, stdin); printf
("%s", message The string under message is printed twice :|. Is there a way to prevent this?
View 5 Replies
View Related
Apr 20, 2013
Code:
char line[BUFSIZ];
while ( fgets(line, sizeof line, file) != NULL ){
llen = strlen(line);
printf("%d - %s
",llen,line);
}
I get a full line printed but my llen is the size of my buffer. how do i get the total size om my line from the beginning to " "
View 3 Replies
View Related
Mar 31, 2013
Why will this code not work? It cashes the program on me once i get to it...
Code:
memset(&input, 0, sizeof(input));
printf("Enter your firstname: ");
pinput = fgets(pinput, BUFF, stdin);
strncpy((pct + *pctcounter-1) ->firstName,pinput , strlen(pinput) - 1);
View 5 Replies
View Related
Dec 21, 2014
Is it possible to fgets() the string (or word) only after a delimiter? I yes then how?
Example: Code: printer, scanner, machine
Also, how can I sscanf() a string with an indefinite number of sizes and assign it to only one variable?
Example:
Code:
str = "I Love C programming 9000";
sscanf(str, "%s %d", strvar, intvar);
View 13 Replies
View Related
Oct 12, 2014
Intead of using scanf("%d",&a) to take a input from the user,how to take a input using fgets(buffer, BUFFERSIZE , stdin) and atoi?
View 1 Replies
View Related
Mar 12, 2013
i am trying to read a string using fgets and storing in an array i want to prevent fgets from storing the new line character on the array using the shortest means possible..
View 2 Replies
View Related
Sep 16, 2013
I have prepared a file through the use of following code
Code:
fprintf(file2, "%i %i %i %i %i %i
",
msg_id,
msg_size,
msg_period,
msg_deadline,
msg_producer,
msg_comsumer
);
As one can see, this file has tab separated integer entries. The file is written correctly, let us call this file "msg.txt".
I face the problem when I read this file, using fgets as follows:
Code:
char singleMessage[100];
while( fgets(singleMessage, sizeof(singleMessage), file ) )
{
puts(singleMessage);
sscanf(singleMessage, "%i %i %i %i %i %i
",
&first, &second, &third, &fourth, &fifth, &sixth);
fprintf(stderr, "first: %d, second: %d, third: %d, fourth: %d, fifth: %d, sixth: %d
",
first, second, third, fourth, fifth, sixth);
}
but fgets only retrieves until the first, i.e, if the first line in the file reads:
788004425
fgets returns only 78.
Does it have to do with how the file was written in the first place.
View 1 Replies
View Related
Jul 2, 2014
How can i play a mp3 file in the background?How can i rule the volume of the mp3 file?why does delay(0.25) doesnt work in this code?
Code:
FILE *datei;
char line[ANZAHL][LAENGE];
int i ;
datei = fopen ("song.txt", "r");
if (datei != NULL){
for(i = 0; i < ANZAHL;i++) {
delay(0.25);
fgets(line,LAENGE,datei);
printf ("%s
[code]....
View 2 Replies
View Related
May 21, 2014
I'm new to C/C++. I am attemping to use fgets and sscanf to read a line of input, and confirm it is a positive number.My code works great, except for the case of a negative number. When I enter a negative number, my while loop seems to run infinitely, with stdin providng the same input over and over again.
Here's the code snippet:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TEXT_LEN 64
void foo() {
char* inStr = (char*)malloc(sizeof(char)*TEXT_LEN);
memset(inStr, 0, TEXT_LEN);
}
[code]....
View 9 Replies
View Related
Oct 31, 2014
#include<stdlib.h>
#include<stdio.h>
#include <string.h>
int main() {
char input[256];
char buffer[256];
[Code] ....
The output of this short program is really weird. When I type 123 from the keyboard,I get the following answer in console window"123 length=4".Why the output of length is always 1 more than the actual length of the string that I type in.
View 2 Replies
View Related
Apr 13, 2014
How can I make fgets stop reading when it reaches a new line? Right now it will read the new line and continue until the buffer is full. I was thinking something like this.
Code:
while(fp!='
'){
fgets(password, 256, fp);
}
View 2 Replies
View Related
May 27, 2013
I am trying to use fgets to read in a string, but keep getting a "no conversion function from std::string to char" error.
View 2 Replies
View Related
Feb 17, 2013
In the assignment we are forbidden to use fscanf(). I have been trying to get this to work, but I've started to realize that I do not have a complete understanding of what strtok() actually does. I'm getting this warning when debugging: "assignment makes integer from pointer without cast."
This warning happens when assigning str to goal and assist, and I think it is because they are, when dereferenced, integers. The code below correctly assigns the name into the correct spot, but leaves nonsense data in the goal and assist arrays.
ex:-7880, -7888 file example: NAME GOALS ASSISTS JOHN 1 2
Code:
void readLinesFromFile( FILE* Ptr, int* goal, int* assist, char** name, int lines ){/*
* Reads lines from files and populates the arrays with the corresponding info.
*/
int index;
char hold[ MAX_LINE ] = { 0 };
char* str = NULL;
[Code] .....
From what I understand about strtok(), it returns a string, and takes in a character array and a key value that tells it when to stop. In the online examples I've seen, they use NULL in the first field. I'm not sure why.
View 5 Replies
View Related
Jan 22, 2014
I have this
Code:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int check_up(char string[]);
int check_low(char string[]);
void to_up(char string[]);
void to_low(char string[]);
[Code] .....
When I compile this I have the following problems: warning: data definition has no type or storage class [enabled by default] in 'to_up(word)'conflicting types in 'to_up' function and to_low function warning: data definition has no type or storage class [enabled by default] into_up function error: unknown type name "word" in line 'printf("All uppercase %s. ", word):;'warning: parameter names (without types) in function declaration [enabled by default] in 'to_up(word)'and 'to_low(word)' 'note: previous declaration of "to_up" was here in function declaration of to_up function
View 7 Replies
View Related
Nov 5, 2014
I'm using fgets which will read a single line at a time, but unlike fgets I don't want it to return the new line char ( ) ?I want to be able to use printf to display the lines next to each other.
View 7 Replies
View Related