Tags down


A question of strstr() and fgets() in a demo from the book《Head First C》

By : Vern
Date : September 16 2020, 06:00 PM
may help you . This is because fgets reads at most the length 80-1 = 79 characters (it leaves 1 character which becomes null termination) but it stops reading after the line feed \n character that gets appended to stdin after you type "wonderful" and press enter. It copies this \n character to your buffer though, so the input string is actually "wonderful\n\0", which doesn't match the data you have, because of the line feed.
Now as you happen to declare the buffer size 10 instead, fgets doesn't have room to store the \n, but still null terminates the string - that's why it works by accident.
code :
fgets(search_for, 80, stdin); 
search_for[strlen(search_for)-1] = '\0';
if(fgets(search_for, 80, stdin) != NULL)
  search_for[strlen(search_for)-1] = '\0';

Share : facebook icon twitter icon

C# Newbie Question from tutorial book: "Head Start C# Greyhound Lab"

By : Sunny Goh
Date : March 29 2020, 07:55 AM
will be helpful for those in need If you need to access something, why don't you just give it access? Like passing it as a argument to the method.
code :
public void MoveThatPic(PictureBox picBox, int distance) // New method, 
                                      // takes a variable called Distance.

    MessageBox.Show(distance.ToString()); // Just show us that Variable.

    // I need to be able to access Form1's picture box before I can use this. :(
    Point BoxMovement = picBox.Location; //create a point called BoxMovement
    BoxMovement.X += distance; // Adjust the X of that by distance.
    picBox.Location = BoxMovement; // now adjust the Box by the Point's location.
private void button2_Click(object sender, EventArgs e)
    PicMover PicMoverObject1 = new PicMover(); // Reserve Space for&Create object
    PicMoverObject1.MoveThatPic(MyPictureBox, 5); // Execute Object Method with a value of 5

classpath question, head first servlets book

By : Akshay Bhange
Date : March 29 2020, 07:55 AM
Does that help Which edition of the book are you using? The /common/lib is true for until with Tomcat 5.5 (Servlet 2.4). Since Tomcat 6.x (Servlet 2.5) and on the Servlet API libraries are available in /lib folder.

fgets & strstr do not seems to work?

By : Kiran
Date : March 29 2020, 07:55 AM
wish helps you The fgets man page says
code :
c [ strcspn(c, "\r\n") ] = 0;

Leetcode 28 - Implement strStr(): question

By : user3064476
Date : March 29 2020, 07:55 AM
To fix this issue I am experiencing a bug in my submissions for Leetcode 28 that has thus far eluded me. My code works for most test cases but I am getting hung up on scenarios such as haystack = "mississippi", needle = "issip". , The problem is that you modify i in
code :
if (haystack[i++] != needle[j]) {
if (haystack[i + j] != needle[j]) {

unexpected result in using fgets and strstr

By : Randolph Judy
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , fgets stores the '\n' - newline (entered when enter key is pressed) in the read buffer as well. So your search string s2 becomes "is\n" which is not present in the s1 to aptr is NULL and that is being printed.
You need to trim the '\n' from s2, you can do that by
Related Posts Related Posts :
  • Efficient Assembly multiplication
  • while (getchar != '\n'); alternative to clear the buffer
  • How to fill a number with a variable amount of 0s?
  • Why C programming gives different output?
  • How to get float value from uint32_t?
  • What is the output of expression c=a+++b? If a=2,b=5 in c language...?
  • Why does it print 1 at the end?
  • Caesar cipher for numbers in C
  • Could someone explain the method of making rules for connect 4 game in C?
  • Is it a memory leak in C when code fails to free memory, but the OS will anyway?
  • Why can't I initialize a loop with i = -1?
  • How does the fork() function behave in if statements?
  • Why strcpy_s is safer than strcpy?
  • gcc flags to disable arithmetic optimisations
  • Recursion in c to print characters
  • Character array initialization using for loop
  • C Programming - Cannot Call Function More Than Once
  • Structures and pointer to pointer element
  • Error when initializing struct members in struct definition
  • Is that fixed-size array? a[]
  • It is not outputting what was expected and am unsure as to the issue
  • i want use C shell code to make buffer overflow and execute bin/sh
  • what the main function in this program returns to os?
  • Suffixes for integer types
  • I don't understand why a equals 1 and b equals 0 in this program
  • C prints other integer number than what it shows on Watches. Why is that?
  • Output of the for loop?
  • How to find 64-bit divisions in existing C code?
  • Finding a numbers with the largest/smallest number of bits set
  • how does " for (int j = 0; j < n || !putchar('\n'); j++) " work
  • handle ctrl+c in client-server socket in c programing
  • Restrict a process to read "/etc/resolv.conf" file in linux
  • Restrict scope of global variable inside one file in C
  • Arduino AES128 encryption - decryption problem
  • Why is not ampersand needed in the following code?
  • Is it possible to define a function name with #define?
  • Why is the size of the data type different when the value is directly passed to the sizeof operator?
  • Segmentation fault / Incorrect checksum triggered ocasionally when reading lines from a file
  • Adding bits at specific indexes for a uint8_t block
  • how can I use "shadow call stack" feature of clang-8 in x86_64?
  • malloc Assertion Failed
  • Array of structs inside of another struct in C
  • Why does this print the wrong part of the string?
  • Moving the cursor and printing characters in console doesn't work with sleep
  • Confusion in adding an element to a sorted list of integers
  • How I can separate integer number in 3 "houses"? Hundred, Ten and Unity
  • User-implemented memory management
  • What it entails to declare and initialize a pointer in this way: int (*p)[n]
  • Function to look though any struct in C
  • Microcontroller not recognized at USB port when connected via pins
  • Unexpected bitshift behavior in C
  • Model 3D spring from points
  • C control reaches end of non void function
  • Tax outputting 0
  • Gtk 3.0 Text View Resizable
  • Statically linking libc into my binary causes it to segfault
  • Child Process Executing Print Statement But Nothing Afterwards
  • Why is the function to find factorial wrong?
  • I am making a prime number checker: when I try to run it, it stopped working
  • I dont understand how this code can display a string by using just getchar
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk