fgets

Function to insert text line by line from a file into string array passed by pointer

喜夏-厌秋 提交于 2019-12-24 10:10:01
问题 I'm trying to create a function read_lines that takes a file *fp, a pointer to char** lines, and pointer to int num_lines. The function should insert each line of text into lines, and increase num_lines to however many lines the file has. Its probably really simple but I've been trying to insert the text for several hours now. This is what main.c would look like. Everything but read_lines is already defined and working. int main(int argc, char* argv[]){ char** lines = NULL; int num_lines = 0;

using fgets() with stdin as input: ^D fails to signal EOF

强颜欢笑 提交于 2019-12-24 09:18:47
问题 I'm writing a program in C on my MacBook which uses Mojave and I'm trying to use fgets() to get a string from stdin. My code compiles - the only issue is that when I run the program in the terminal, after fgets() is called and I type in the desired input, I can't figure out how to signal the end of the input so that the program can continue running. I recognise many people have had this issue and that there are many pages on this site addressing it. But none of the solutions (that I have

ARTS Week 3

痞子三分冷 提交于 2019-12-24 04:01:08
Nov 11,2019 ~ Nov 17,2019 Algorithm 本周来介绍快速求一个数字n次方的余数。 理论基础 我们先定义运算$ x \bmod p = r $与$ x \equiv r \pmod p $的含义是一样的。若$ p = 5 $,则可以将所有整数划分到5个不相交的集合里,具体如下: $$ \left{\begin{matrix} & {\dots -10, -5, 0, 5, 10 \dots } \bmod 5 = 0 & \ & {\dots -9, -4, 1, 6, 11 \dots } \bmod 5 = 1 & \ & {\dots -8, -3, 2, 7, 12 \dots } \bmod 5 = 2 & \ & {\dots -7, -2, 3, 8, 13 \dots } \bmod 5 = 3 & \ & {\dots -6, -1, 4, 9, 14 \dots } \bmod 5 = 4 & \ \end{matrix}\right. $$ 可能有人不懂为什么$-4 \bmod 5 = 1$,我来解释一下,因为$ -4 = 5 \times (-1) + 1 $,故$ -4 \div 5 = -0.8 = -1 \cdots 1 $。因此$-4 \bmod 5 = 1$ 那么根据上面的描述

Why is the following code not allowing me to get user input with fgets yet works with scanf?

前提是你 提交于 2019-12-23 12:48:02
问题 This is a short excerpt from a bigger program, but the rest of the program is irrelevant since I think I was able to isolate the issue. I suspect that it has something to do with the way I'm using fgets. I've read that it's preferable to use fgets over scanf, but I can't seem to get it to work properly here. When I use the following code, the program doesn't give me a chance to enter the number (but simply skips to the while loop which checks if the number entered is in the correct range):

fgets() Not Ignoring New Line

旧巷老猫 提交于 2019-12-23 06:28:28
问题 For my practice assignment I have to use either gets() or fgets(). I chose fgets() as its more secure. The first input is meant to be able to hold a maximum of 5 characters. So i gave the char array a size of 6 to accommodate the trailing '\0'. I found the fgets() issue of it adding a trailing '\n' when you press Enter (using stdin with fgets()) I done a bit of research and found a for loop to try and get rid of it. However, it doesnt seem to be working and i cant for the life of me figure

fgets segmentation fault when reading input from user

大兔子大兔子 提交于 2019-12-23 04:24:27
问题 here's the offending code using ubuntu char *name; int main(void) { fgets(name, sizeof(name), stdin); } void HUD() { printf("%s ", name); } Here's my problem. I started with scanf("%s", &name) and was getting junk at the end of the string. Through the last 2 hours have been reading docs on scanf, and fgets, because apparently scanf shouldn't be used when you don't know the size of the array you want, (and since user input can vary in size) I decided to try using fgets. I've also tried setting

Reading strings from stdin

送分小仙女□ 提交于 2019-12-23 03:09:34
问题 I'm trying to read a line from stdin with this kind of format: Boston "New York" "San Francisco" Memphis (note that string with a space in them are between brackets. Also note that each city name is separated by a space.) I've tried to read this with scanf one at a time, fgets the whole line and then tokenizing, but with bad results. What I pretend is to store everything in a muldimensional char array for later use. Any suggestions on how I should solve this problem? Thank you in advance! 回答1

Input of dynamic size using fgets in C

空扰寡人 提交于 2019-12-23 01:55:25
问题 I want to have a string(can contain spaces) as input. I want to do dynamic allocation. Structure of my program is somewhat like this. #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct msgclient { int msglen; int msgtype; char *cp; }M1; int main() { M1 *m; m=malloc(sizeof(M1)); m->msglen=5; m->msgtype=6; printf("Enter\t"); fgets(m->cp,50,stdin); //here // m->cp[strlen(m->cp)]='\0'; printf("\n%d\n%d\n",m->msglen,m->msgtype); fputs(m->cp,stdout); return 0; } I want to know

Input of dynamic size using fgets in C

早过忘川 提交于 2019-12-23 01:55:10
问题 I want to have a string(can contain spaces) as input. I want to do dynamic allocation. Structure of my program is somewhat like this. #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct msgclient { int msglen; int msgtype; char *cp; }M1; int main() { M1 *m; m=malloc(sizeof(M1)); m->msglen=5; m->msgtype=6; printf("Enter\t"); fgets(m->cp,50,stdin); //here // m->cp[strlen(m->cp)]='\0'; printf("\n%d\n%d\n",m->msglen,m->msgtype); fputs(m->cp,stdout); return 0; } I want to know

How to use fgets() and store every line of a file using an array of char pointers?

一个人想着一个人 提交于 2019-12-22 17:41:35
问题 I wrote the program below in order to read every line of the file and then store each line in a array of char pointers for later use (like sorting only the pointers not the real strings). But the problem is that it keeps storing the same line. Lets say that the file is called file.txt and has two(2) lines with text. first sentence second sentence The code I wrote is the following: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <unistd.h> int main(){