我有一个txt文件,其中包含:
1:2
2:5
3:10
4:1
我需要能够添加这些数字。例如,我想添加到最后一行+5:
1:2
2:5
3:10
4:6
我怎样才能做到这一点?我想知道是否正确的方法是将文件输入到数组中,但我不知道如何执行此操作,因为我需要将数字分成键和值,我猜?
答案 0 :(得分:1)
所描述的问题非常通用,但我可以做一些假设来帮助你。
初始txt文件基本上是键值存储:
#include "stdafx.h"
#include "Win32Project1.h"
#include "Browser.h"
HWND mainHWND;
WebBrowser* myBrowser = NULL;
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
OleInitialize(NULL);
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
myBrowser = new WebBrowser(mainHWND);
//myBrowser->Navigate(L"https://google.com.vn");
myBrowser->SetText(L"Hello!");
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32PROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
mainHWND = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!mainHWND)
{
return FALSE;
}
ShowWindow(mainHWND, nCmdShow);
UpdateWindow(mainHWND);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
// Removed
return (INT_PTR)FALSE;
}
假设第一个数字是关键字,您应该告诉您的算法使用关键字1:2
2:5
3:10
4:1
对该行执行+x
求和。
在(未经测试和未优化的)PHP代码中:
n
然后将其称为:
function addToStore($destKey, $numberToSum) {
$newStore = [];
foreach(file('store.txt') as $line) {
list($key, $value) = explode(':', $line);
if( $key === $destKey) {
$value += $numberToSum;
}
newStore[] = "$key:$value";
}
file_put_contents('store.txt', implode("\n", $newStore));
}
答案 1 :(得分:0)
这样的事情:
// get from file
$file = file('file.txt');
$array = [];
foreach ($file as $line) {
list($column1, $column2) = explode(':', $line);
$array[(int) $column1] = (int) $column2;
}
// add number
$array[4] += 5;
// save to file
$content = '';
foreach ($array as $column1 => $column2) {
$content .= $column1 . ":" . $column2 . "\n";
}
file_put_contents('file.txt', $content);
但是,您必须确保第一列中的索引是唯一的,否则将跳过某些行(实际被覆盖)。
答案 2 :(得分:0)
是的,当您以数组形式获得数字时,可以为其添加数字,并且您可以按照下面给出的键/值格式保存文本文件数据。
$ file_array = array();
$ myfile = fopen(" yourtextfile.txt"," r")或 死("无法打开文件!");
//读取一行直到文件结尾
while(!feof($ myfile)){
$ line = fgets($ myfile);$ line_array = explode(":",$ line); //拆分每一行以制作数组
$ file_array [$ line_array [0]] = $ line_array [1];
}
FCLOSE($ MYFILE); // file_array应该将文件数据显示为数组
print_r($ file_array);
希望这可以帮助你!!