c语言简单文本编辑器
A. 如何用c语言做简易文本编辑器(最好能有代码和说明)
fopen,fgets ,然后统计单词书,转换。
B. 用c语言设计一个简单文本编辑器
友情帮顶
C. 设计一个简单的文本编辑器,用C语言实现(高分悬赏)
http://directory.fsf.org/category/editors/
这里全是文本编辑器的免费源代码,自己挑吧
D. 用C语言编写一个简单的文本编辑器.
我的C语言是自学的,懂一小点。虽然我没有你说的那种源代码,但我有记事本的源代码,你想看看吗?
记事本(主程序)
#include <windows.h>
#include "sample.h"
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE g_hInst = NULL;
#define IDC_MAIN_TEXT 1001
BOOL LoadFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwFileSize;
dwFileSize = GetFileSize(hFile, NULL);
if(dwFileSize != 0xFFFFFFFF)
{
LPSTR pszFileText;
pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
if(pszFileText != NULL)
{
DWORD dwRead;
if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
{
pszFileText[dwFileSize] = 0;
if(SetWindowText(hEdit, pszFileText))
bSuccess = TRUE;
}
GlobalFree(pszFileText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL SaveFile(HWND hEdit, LPSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwTextLength;
dwTextLength = GetWindowTextLength(hEdit);
if(dwTextLength > 0)
{
LPSTR pszText;
pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
if(pszText != NULL)
{
if(GetWindowText(hEdit, pszText, dwTextLength + 1))
{
DWORD dwWritten;
if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
bSuccess = TRUE;
}
GlobalFree(pszText);
}
}
CloseHandle(hFile);
}
return bSuccess;
}
BOOL DoFileOpenSave(HWND hwnd, BOOL bSave)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH];
ZeroMemory(&ofn, sizeof(ofn));
szFileName[0] = 0;
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "文本文件 (*.txt)\0*.txt\0所有文件 (*.*)\0*.*\0\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = "txt";
if(bSave)
{
ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT;
if(GetSaveFileName(&ofn))
{
if(!SaveFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "保存文件失败.", "错误信息",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
else
{
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
if(GetOpenFileName(&ofn))
{
if(!LoadFile(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName))
{
MessageBox(hwnd, "打开文件失败.", "错误信息",
MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
}
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_CREATE:
CreateWindow("EDIT", "",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE |
ES_WANTRETURN,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL);
SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT,
(WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0));
break;
case WM_SIZE:
if(wParam != SIZE_MINIMIZED)
MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam),
HIWORD(lParam), TRUE);
break;
case WM_SETFOCUS:
SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT));
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case CM_FILE_OPEN:
DoFileOpenSave(hwnd, FALSE);
break;
case CM_FILE_SAVEAS:
DoFileOpenSave(hwnd, TRUE);
break;
case CM_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
case CM_ABOUT:
MessageBox (NULL, "欢迎使用jiyingjun的源代码" , "关于...", 0);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClass;
HWND hwnd;
MSG Msg;
g_hInst = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = g_hInst;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
WndClass.lpszMenuName = "MAINMENU";
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass))
{
MessageBox(0, "注册窗口失败", "错误信息",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"记事本",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
NULL, NULL, g_hInst, NULL);
if(hwnd == NULL)
{
MessageBox(0, "创建窗口失败", "错误信息",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
记事本(资源文件)
#include "sample.h"
A ICON MOVEABLE PURE LOADONCALL DISCARDABLE "sample.ico"
MAINMENU MENU
{
POPUP "文件(&F)"
{
MENUITEM "打开(&O)...", CM_FILE_OPEN
MENUITEM "另存为(&S)...", CM_FILE_SAVEAS
MENUITEM SEPARATOR
MENUITEM "关闭", CM_FILE_EXIT
}
POPUP "帮助(&H)"
{
MENUITEM "关于(&A)", CM_ABOUT
}
}
记事本(头文件)
#define CM_FILE_SAVEAS 9072
#define CM_FILE_EXIT 9071
#define CM_FILE_OPEN 9070
#define CM_ABOUT 9069
E. 用c语言设计一个简单的文本编辑器,如何编写程序代码
你是要制作出一个类似于微软的文本文档的东西吗?
F. c语言编写简易的文本编辑器
我这里有一个功能强大文本编译器程序的完整c代码,是外国人写的。不好意思,很长,发不上来。
不过这里有一个简易文本编译器。虽说是简易,也不是那么好弄的,给你:
http://..com/question/79338502.html
G. C语言文本编辑器
sprintf(buf,"edit %s",s);
将字符串s格式化到buf中,代码中,s是用户scanf输入的内容,然后在输入内容前面加上edit命令,存入buf。
system(buf);
system是调用操作系统的shell命令,说简单点就是调用操作系统的命令行(windows里,开始->运行->cmd可以看到命令行界面),这里调用的命令是edit,只要用户输入好相应的文本文件路径,就能打开edit进行编辑。从后面的命令cls来看,你用的系统应该是windows或者dos。
H. C语言简单行编辑器
/*
c语言程序设计 简单的行编辑器
【要求】
(1) 设置一个简单的行编辑器,每行以回车结束
(2) 数据以文件形式存储
(3) 编辑器具有查找、替换、修改数据的功能
【备注】完全原创,编写时间:2010-7-13。请把所有的注释信息提取出来就可以写程序设计报告。
*/
#include /*标准文件流操作,这里使用了fopen/fclose/fprintf/printf/scanf/gets函数*/
#include /*标准系统库,这里使用了malloc/free/exit*/
#include /*标准字符串库,这里使用strlen/strcpy/memcpy/memset*/
#define szLINE 252 /*定义一行字符串最长为252字节*/
#define CMDS 12 /*定义12个标准行编辑命令*/
/*采用链表存储文本*/
typedef struct LINE {
char text[szLINE]; /*文本内容*/
struct LINE * next; /*链表指针*/
} L;
/*简写无类型整数*/
typedef unsigned int U;
/*定义12个行编辑命令的标准格式*/
typedef void (*FUNC)(L **, char*);
/*定义12个标准行编辑命令的关键字*/
char keywords[CMDS][8]={
"quit", "help", "load", "save",
"view", "count", "append", "insert",
"erase", "edit", "lookup", "replace"
};/*end keywords*/
/*清空链表操作*/
void clear(L ** lines)
{
L * a = 0, * b = 0;
if(!lines) return ;
a = *lines;
while(a) {
b = a->next ;
free(a);
a = b;
}/*end while*/
*lines = 0;
}/*end clear*/
/*在链表中根据行号index调出指定的行*/
L * locate(L * lines, U index)
{
L * t = lines; U i = 0;
if(!t) return 0;
if(index == 0) return t;
for(i = 0; i < index; i++) {
t = t->next;
if(!t) return 0;
}/*next*/
return t;
}/*end locate*/
/*浏览命令,如果f存在则以带行号格式保存文件(如果f==stdout则打印到屏幕上),
浏览范围为from到to(行号)。view(lines, 0, 0, 0)表示统计已加载到内存的文本行数量*/
int view(L * lines, FILE * f, U from, U to)
{
L * t = lines; U index = 0;
while(t) {
index ++;
if(f && index >= from && index text);
t = t->next;
}/*end while*/
return index;
}/*end view*/
/*在当前文档中根据关键字进行搜索,并将搜索结果打印出来*/
void lookup(L * lines, char * string)
{
L * t = 0; U index = 0;
if(!string) return ;
t = lines;
while(t) {
index ++;
if(strstr(t->text , string)) printf("%d: %s", index, t->text );
t=t->next;
}/*end while*/
}/*end lookup*/
/*在一行文本中执行替换命令,把所有关键字替换为新关键字*/
void rpc(char * string, char * key, char * replacement)
{
char fine[szLINE], * x = 0, * y = 0, * z = 0;
int la = 0, lb = 0, r = 0;
if(!string || !key || !replacement) return ;
memset(fine, 0, szLINE);
x = string; y = fine;
/*首先记录新旧关键字长度*/
la = strlen(key);
lb = strlen(replacement);
do {
/*用指针逐个比较*/
r = memcmp(x, key, la);
if(r) {/*如果关键字不匹配则复制字符串*/
*y = *x;
x++; y++;
}else{/*如果关键字匹配则替换字符串*/
memcpy(y, replacement, lb);
x += la; y += lb;
}/*end if*/
}while(*x);
/*将替换完成的结果返回*/
memcpy(string, fine, szLINE);
}/*end rpc*/
/*全文替换*/
void replace(L * lines, char * string, char * replacement)
{
L * t = 0; U index = 0;
if(!string || !lines || !replacement) return ;
t = lines;
while(t) {
index ++;
if(strstr(t->text , string)) {
printf("[BEFORE] %d: %s", index, t->text );
rpc(t->text, string, replacement);
printf("[AFTER ] %d: %s", index, t->text );
}/*end if*/
t=t->next;
}/*end while*/
}/*end replace*/
/*根据行号插入一行新文本,如果行号小于零则将文本追加至链表尾*/
void insert(L ** lines, char * line, int index)
{
L * t = 0, * s = 0; int i = 0;
if(!lines || !line) return ;
/*首先为新文本分配一个链表节点*/
t = (L*)malloc(sizeof(L));
memset(t, 0, sizeof(L));
strcpy(t->text , line);
if(index == 0 || !*lines) {/*如果链表为空则以新节点为起点定义链表*/
t->next = *lines;
*lines = t;
return ;
}/*end if*/
s = *lines;
if(index > 0)/*如果行号为正整数,则将链表指针指向行号之前*/
for(i = 0; i < index-2; i++) {
if(!s->next ) break;
s = s->next ;
}/*next*/
else/*否则链表指针指向表尾*/
while(s->next ) s = s->next ;
/*end if*/
/*完成链表插入操作*/
if(s->next ) t->next = s->next ;
s->next = t;
}/*end insert*/
/*根据行号删除一行文本*/
void erase(L ** lines, U index)
{
L * a = 0, * b = 0, * c = 0;
if(!lines) return ;
/*index -1 表示目标行,index -2表示目标行的前一行*/
a = locate(*lines, index-2);
b = locate(*lines, index-1);
if(!b) return ;
if(a) /*如果前一行存在则删除目标行*/
a->next = b->next;
else/*否则表示表头删除*/
*lines = b->next ;
/*end if*/
/*释放内存*/
free(b);
}/*end erase*/
/*根据行号和新录入文本替换原有行*/
void edit(L * lines, char * line, U index)
{
L * t = locate(lines, index-1);
if(!t) return ;
if(line) strcpy(t->text , line);
}/*end edit*/
/*将文件整个装入链表*/
int load(L ** lines, char * file)
{
FILE * f = 0; char line[szLINE]="";
int total = 0;
if(!lines || !file) return 0;
clear(lines);/*首先清空链表*/
/*打开文件*/
f = fopen(file, "r");
if(!f) {
fprintf(stderr, "%s is bad.\n", file);
return 0;
}/*end if*/
/*逐行读入内存并插入表尾*/
while(!feof(f)) {
memset(line, 0, szLINE);
fgets(line, szLINE - 1, f);
insert(lines, line, -1);
total ++;
}/*end while*/
fclose(f);
fprintf(stderr, "[%s] %d lines loaded.\n", file, total);
/*返回总行数*/
return total;
}/*end load*/
/*将链表保存到指定的文本文件*/
int save(L * lines, char * file)
{
FILE * f = 0; L * t = lines;
int total = 0;
if(!lines || !file) return 0;
/*打开文件*/
f = fopen(file, "w");
if(!f) {
fprintf(stderr, "%s is bad.\n", file);
return 0;
}/*end if*/
t = lines;
while(t) {/*逐个文件写入*/
fprintf(f, "%s", t->text );
t = t->next ;
total ++;
}/*end while*/
fclose(f);
fprintf(stderr, "[%s] %d lines saved.\n", file, total);
/*返回总行数*/
return total;
}/*save*/
/*执行加载文本文件命令*/
void exec_load(L ** lines, char * line)
{
char cmd[szLINE] = "", file[szLINE] = "";
/*分析命令行,提取文件名*/
sscanf(line, "%s %s", cmd, file);
/*执行加载命令*/
load(lines, file);
}/*end exec_load*/
/*执行文本保存命令*/
void exec_save(L ** lines, char * line)
{
char cmd[szLINE] = "", file[szLINE] = "";
/*分析命令行,提取文件名*/
sscanf(line, "%s %s", cmd, file);
/*执行保存命令*/
save(*lines, file);
}/*end exec_save*/
/*执行文本查看命令*/
void exec_view(L ** lines, char * line)
{
char cmd[szLINE] = ""; U from = 0, to = 0;
/*分析命令行,提取目标要查看的起始行号和终止行号*/
sscanf(line, "%s %u %u", cmd, &from, &to);
/*如果起始行号和终止行号大小相反,则根据起始行号显示一页*/
if(to < from) to = from + 24;
/*执行查看命令*/
view(*lines, stdout, from, to);
}/*end exec_view*/
/*执行行数统计命令*/
void exec_count(L ** lines, char * line)
{
fprintf(stderr, "%d lines in mem.\n", view(*lines, 0, 0, 0));
}/*end count*/
/*执行文本追加命令*/
void exec_append(L ** lines, char * line)
{
char text[szLINE] = "";
/*在命令之后另起新行用于录入文本*/
gets(text); strcat(text, "\n");
/*执行文本追加命令*/
insert(lines, text, -1);
}/*end exec_append*/
/*执行文本插入命令*/
void exec_insert(L ** lines, char * line)
{
char cmd[szLINE] = "", text[szLINE] = ""; U index = 0;
/*从命令行提取目标插入点的行号*/
sscanf(line, "%s %d", cmd, &index);
/*在命令之后另起新行用于录入文本*/
gets(text); strcat(text, "\n");
/*执行文本插入命令*/
insert(lines, text, index);
}/*end insert*/
/*执行文本删除命令*/
void exec_erase(L ** lines, char * line)
{
char cmd[szLINE] = ""; U index = 0;
/*从命令行提取目标行号*/
sscanf(line, "%s %d", cmd, &index);
/*执行文本删除命令*/
erase(lines, index);
}/*end erase*/
/*执行文本编辑命令*/
void exec_edit(L ** lines, char * line)
{
char cmd[szLINE] = "", text[szLINE] = ""; U index = 0;
/*从命令行提取目标行号*/
sscanf(line, "%s %d", cmd, &index);
/*在命令之后另起新行用于录入文本*/
gets(text); strcat(text, "\n");
/*执行文本编辑命令*/
edit(*lines, text, index);
}/*end edit*/
/*执行文本检索命令*/
void exec_lookup(L ** lines, char * line)
{
char cmd[szLINE] = "", text[szLINE] = "";
/*从命令行提取关键字*/
sscanf(line, "%s %s", cmd, text);
/*执行文本检索命令*/
lookup(*lines, text);
}/*end lookup*/
/*执行在线帮助命令*/
void exec_help(L ** lines, char * line)
{printf("\tcommands:\n\thelp\n\tquit\n\
\tload [file.txt]\n\
\tsave [file.txt]\n\
\tview [from line] [to line]\n\
\tcount\n\
\tappend [return + text]\n\
\tinsert [line number] [return + text]\n\
\terase [line number]\n\
\tedit [line number] [return + text]\n\
\tlookup [text]\n\
\treplace [keyword] [replacement]\n");
}/*end help*/
/*执行文本替换命令*/
void exec_replace(L ** lines, char * line)
{
char cmd[szLINE] = "", text[szLINE] = "", key[szLINE]="";
/*从命令行提取新旧关键字*/
sscanf(line, "%s %s %s", cmd, key, text);
/*执行文本替换命令*/
replace(*lines, key, text);
}/*end replace*/
/*执行退出命令*/
void exec_quit(L ** lines, char * line){exit(0);}
/*行编辑命令执行函数,顺序与关键字表keywords一一对应*/
FUNC functions[CMDS]={
exec_quit, exec_help, exec_load, exec_save,
exec_view, exec_count, exec_append, exec_insert,
exec_erase, exec_edit, exec_lookup, exec_replace
};/*end functions*/
/*从行输入中识别关键字,相当于parse*/
int identified(char * command, char * key)
{
int ln = 0, r = 0;
if(!command || !key) return 0;
ln = strlen(key);
r = memcmp(command, key, ln);
return r==0;
}/*end identified*/
/*主函数*/
int main(int argc, char * argv[])
{
L * lines = 0; char line[szLINE]=""; int a = 0, b = 0, i = 0; FUNC fun = 0;
/*打印欢迎信息*/
printf("Welcome to LINE EDITOR V1.0\nCommand [help] is available.\n");
/*如果带主函数带参数,则可以用于打印帮助,或者根据该参数加载一个文本文件*/
if(argc > 1) {
a = strcmp(argv[1], "--help");
b = strcmp(argv[1], "/h");
if(a && b)
load(&lines, argv[1]);
else{
exec_help(0, 0);
return 0;
}/*end if*/
}/*end if*/
/*主命令循环*/
for(;;) {
/*命令提示符中间是表示当前载入的文档总共有多少行的意思*/
printf("\n