Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8671639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:59:51+00:00 2026-06-12T18:59:51+00:00

I have a problem with fgets . I can’t save the String temp in

  • 0

I have a problem with fgets. I can’t save the String “temp” in the if-statement.

The Code:

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include<stdlib.h>
      4 
      5 void moep(char** tmp){
      6         FILE *fp;
      7         fp=fopen("moep", "r");
      8         int line_num = 1;
      9         int find_result = 0;
     10         char* str="jochen";
     11         char temp[512];
     12 
     13 while(fgets(temp, 512, fp) != NULL) {
     14                 if((strstr(temp, str)) != NULL) {
     15                         printf("A match found on line: %d    \n", line_num);
     16                         tmp[find_result]=temp;
     17                         printf("\n%s\n", tmp[find_result]    );
     18                         find_result++;
     19                 }
     20                 line_num++;
     21         }
     22 fclose(fp);
     23 printf("tmp[0]:%s\n",tmp[0]);
     24 tmp[1]="ich funktioniere";
     25 }
     26 
     27 int main(){
     28 
     29         char* tmp[1];
     30         moep(tmp);
     31         printf("%s, %s\n",tmp[0],tmp[1]);
     32         return 0;
     33 }

and moep is:

unwichtig1
jochen g
unwichtig2
unwichtig3

The output:

 /A match found on line: 2

jochen g

tmp[0]:unwichtig3

���, ich funktioniere

why can’t I save “jochen” in tmp[0]?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:59:52+00:00Added an answer on June 12, 2026 at 6:59 pm

    Assigning to tmp[1] is assigning to random memory, as you’re array isn’t that big.
    It will have all sorts of bad side effects – including crashing the program.

    You are also reusing the temp[] array – so that will keep getting overwritten, you need to copy the string if you want to keep it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a really weird problem with fgets() in C. Below is the code
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have problem with show or hide form in Window Form Application. I start
I have problem with UIWebView delay when the load image from url. In my
I have problem while loading data into html select when users press or click
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have problem developing with live555. I already build the lib-files and example projects
I have problem with TableView its empty. In (.h) file: @interface TableViewController : UITableViewController{

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.