我想出了一个提高词汇量的想法。这个想法是在一个文件中包含大量最常见的英语单词。 然后我会编写一个程序,一次在屏幕上显示一个单词。如果我认识这个单词,我按向下键 移至下一个单词,否则我按“S”将该单词保存到名为 Unknown.txt 的文本文件中。
当我完成时,我将收集所有我不知道其含义的单词。如果我停在这里,并手动浏览每个单词 并用我的字典查找它的含义,这样学习它们将花费大量时间。
但是,如果我有一种方法可以以编程方式保存该单词的含义, 我可以轻松打开文件并立即了解单词的含义。这就是我想要实现的目标。
“10kword.txt”文件如下所示:
purchase
customers
active
response
practice
hardware.
这是我到目前为止的代码:
#include <stdio.h>
#include <Windows.h>
void cls(void *hConsole);
int main(void)
{
FILE *inp, *out;
if (fopen_s(&inp, "10kWords.txt", "r")) {
fprintf(stderr, "Unable to open input file\n");
return 1;
}
else if (fopen_s(&out, "Unknown.txt", "a")) {
fprintf(stderr, "Error opening file Unknown.txt\n");
fclose(inp);
return 1;
}
char buf[100];
void *hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
while (1) {
//Press the down key to move to the next word
if (GetAsyncKeyState(VK_DOWN) == -32767) {
cls(hConsole);
fscanf_s(inp, "%s", buf, 100);
printf("%s", buf);
}
//Press S to save the word to output file
else if (GetAsyncKeyState('S') == -32767) {
fprintf(out, "%s\n", buf);
//Obtain word meaning from dictionary Programatically HERE and print it to 'out'
}
else if (GetAsyncKeyState(VK_ESCAPE)) {
break;
}
}
fclose(inp);
fclose(out);
return 0;
}
void cls(void *hConsole)
{
COORD coordScreen = { 0, 0 }; // home for the cursor
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
// Get the number of character cells in the current buffer.
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
// Fill the entire screen with blanks.
if (!FillConsoleOutputCharacter(hConsole, // Handle to console screen buffer
(TCHAR) ' ', // Character to write to the buffer
dwConSize, // Number of cells to write
coordScreen, // Coordinates of first cell
&cCharsWritten))// Receive number of characters written
{
return;
}
// Get the current text attribute.
if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
{
return;
}
// Set the buffer's attributes accordingly.
if (!FillConsoleOutputAttribute(hConsole, // Handle to console screen buffer
csbi.wAttributes, // Character attributes to use
dwConSize, // Number of cells to set attribute
coordScreen, // Coordinates of first cell
&cCharsWritten)) // Receive number of characters written
{
return;
}
// Put the cursor at its home coordinates.
SetConsoleCursorPosition(hConsole, coordScreen);
}
最佳答案
似乎您正在寻找的是一个字典 API,您可以将请求发送到服务器并获得响应。快速google search显示有相当多。除此之外,您还需要一些额外的 C 库来发出 HTTP 请求,例如 libcurl ,以及从 this other stack overflow question 解析 JSON 或 XML 的方法您有一个如何使用它的示例,您可以从那里使用它向 Merriam 的 API 发送请求。
这绝对是一个开放式问题,但希望我已经为您指明了正确的方向。
这是一些示例代码 from the curl website展示如何使用 Curl 执行 get http 请求:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
int main(void)
{
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.dictionaryapi.com/api/v1/references/collegiate/xml/overflow?key=<YOUR KEY GOES HERE>");
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(curl_handle);
/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
}
else {
/*
* Now, our chunk.memory points to a memory block that is chunk.size
* bytes big and contains the remote file.
*
* Do something nice with it!
*/
printf("%lu bytes retrieved\n", (long)chunk.size);
}
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
free(chunk.memory);
/* we're done with libcurl, so clean it up */
curl_global_cleanup();
return 0;
}
此示例会将溢出定义作为 XML 文件打印到内存中。
祝你好运!
关于c - 以编程方式查找单词含义并将其打印到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36060661/