LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2022, 07:16 AM   #1
errigour
Member
 
Registered: May 2009
Posts: 369

Rep: Reputation: 6
fscanf question i guess


Why doesn't this print the game time? What am I doing wrong? The last two printf lines in main do not print and when I compile it on my server I get this error:
Code:
      1 [main] a 2917 C:\cygwin64\home\Errigour\c src\web server\a.exe: *** fatal error - Internal error: TP_NUM_C_BUFS too small: 50
                                           Hangup
Also I think it is because of the fscanf in load_game_time()

Code:
#include <stdio.h>

const char *days[] = {
    "Beuth",  // Beginning truth
    "Douth",  // Doubt truth
    "Bejewl", // Believe jewel
    "Fevif",  // Forever life
    "Not"     // No hate
};

const char *months[] = {
    "Underus\0",
    "Dosendris\0",
    "Treviator\0",
    "Quatrin\0",
    "Cincalm\0",
    "Seisol\0",
    "Setten",
    "Ochace",
    "Nuavor",
    "Dieoon"
};

struct game_time {
    unsigned int second;
    unsigned int minute;
    unsigned int hour;
} gtime;

struct game_date {
    unsigned int day;
    unsigned int dayofmonth;
    unsigned int month;
    long long unsigned int year;
}gdate;

void save_game_time(){
    FILE *fp;
    if ((fp=fopen("time.db", "w")) == 0)
        perror("Error saving time.db!");
    fprintf(fp, "%u %u %u %u %u %u %llu", gtime.second, gtime.minute, gtime.hour, gdate.day, gdate.dayofmonth, gdate.month, gdate.year );
    fclose(fp);
}

void load_game_time(){
    FILE *fp;
    if ((fp=fopen("time.db", "r")) == 0)
        perror("Error loading time.db!");
    fscanf(fp, "%u %u %u %u %u %u %llu", gtime.second, gtime.minute, gtime.hour, gdate.day, gdate.dayofmonth, gdate.month, gdate.year );
    fclose(fp);
}

int main()
{
    char get[3], path[3], http[4];
    char request[]="GET tmp http\0";
    
    setbuf(stdout, NULL);
    
    sscanf(request, "%s %s %s", get, path, http);
    printf("%s\n", get);
    printf("<html lang=\"en\">\n"
                        "<title>Space</title>\n"
                        "<head>\n"
                        "</head>\n"
                        "<body>\n"
                        "A pagan says, \"Finally, Hello world!\"<br>\n"
                        "<image src=\"favicon.ico\" alt=\"favicon.ico\" height=\"25\" width=\"25\">\n"
                        "<form method=\"post\" action=\"index.html\">\n"
                        "    <label for=\"email\">Username:</label><input type=\"text\" id=\"email\" name=\"email\" value=\"\"><br>\n"
                        "    <label for=\"pass\">Password:</label><input type=\"password\" id=\"pass\" name=\"pass\" value=\"\"><br>\n"
                        "<input type=\"submit\" value=\"Login\\Create\">\n"
                        "</form>\n"
                        "</body>\n"
                        "</html>\n\0");
    printf("Size of char: %d\n", sizeof(char));
    printf("Got here 1\n");
    save_game_time();
    load_game_time();
/* The following two lines do not print */
    printf("Got here 2\n");
    printf("%d:%d:%d on %s of %s\n", gtime.hour, gtime.minute, gtime.second, days[gdate.day], months[gdate.month]);
}
 
Old 05-05-2022, 08:24 AM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,158
Blog Entries: 6

Rep: Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836
Well, this is linux forum.

I compiled that code with:
Code:
gcc test.c -o test
And I get
Code:
./test
GETtmphttp
<html lang="en">
<title>Space</title>
<head>
</head>
<body>
A pagan says, "Finally, Hello world!"<br>
<image src="favicon.ico" alt="favicon.ico" height="25" width="25">
<form method="post" action="index.html">
    <label for="email">Username:</label><input type="text" id="email" name="email" value=""><br>
    <label for="pass">Password:</label><input type="password" id="pass" name="pass" value=""><br>
<input type="submit" value="Login\Create">
</form>
</body>
</html>
Size of char: 1
Got here 1
Segmentation fault (core dumped)
 
Old 05-05-2022, 08:27 AM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,158
Blog Entries: 6

Rep: Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836Reputation: 1836
Compiled it again with line 79
Code:
//load_game_time();
Code:
./test
GETtmphttp
<html lang="en">
<title>Space</title>
<head>
</head>
<body>
A pagan says, "Finally, Hello world!"<br>
<image src="favicon.ico" alt="favicon.ico" height="25" width="25">
<form method="post" action="index.html">
    <label for="email">Username:</label><input type="text" id="email" name="email" value=""><br>
    <label for="pass">Password:</label><input type="password" id="pass" name="pass" value=""><br>
<input type="submit" value="Login\Create">
</form>
</body>
</html>
Size of char: 1
Got here 1
Got here 2
0:0:0 on Beuth of Underus
 
Old 05-05-2022, 09:27 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,885
Blog Entries: 1

Rep: Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872Reputation: 1872
Code:
-   fscanf(fp, "%u %u %u %u %u %u %llu", gtime.second, gtime.minute, gtime.hour, gdate.day, gdate.dayofmonth, gdate.month, gdate.year );
+   fscanf(fp, "%u %u %u %u %u %u %llu", &gtime.second, &gtime.minute, &gtime.hour, &gdate.day, &gdate.dayofmonth, &gdate.month, &gdate.year );
 
2 members found this post helpful.
Old 05-05-2022, 12:06 PM   #5
errigour
Member
 
Registered: May 2009
Posts: 369

Original Poster
Rep: Reputation: 6
Oh man thanks that fixed the problem completely.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] binutils configure.guess - cannot guess build type bisonapp Linux From Scratch 3 01-09-2011 03:31 AM
fscanf & fgetc help billybob2004 Programming 2 02-04-2004 10:24 AM
C fscanf / feof question. Claus Programming 1 02-02-2004 11:55 PM
fscanf c programming tomato Programming 14 01-09-2004 08:33 PM
using fscanf function Linh Programming 2 07-14-2003 12:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration