導航:首頁 > 文件管理 > 配置文件讀寫的案例

配置文件讀寫的案例

發布時間:2025-10-10 23:31:06

1. vc++怎樣從excel文件中讀寫數據

轉載:

首先利用Visual C++ 6.0,建立一個MFC基於對話框的工程項目,共享DLL,Win32平台。工程名稱ExcelTest。在主對話框中加入一個按鈕,
ID IDC_EXCELTEST
Caption Test Excel
雙擊該按鈕,增加成員函數void CExcelTestDlg::OnExceltest()。
在BOOL CExcelTestApp::InitInstance()中,dlg.DoModal();之前增加代碼
if (CoInitialize(NULL)!=0)
{
AfxMessageBox("初始化COM支持庫失敗!");
exit(1);
}
在return FALSE; 語句前,加入:
CoUninitialize();
選擇Menu->View->ClassWizade,打開ClassWizade窗口,選擇Add Class->From a type library,選擇D:\Program Files\Microsoft Office\office\Excel9.OLB(D:\Program Files\Microsoft Office\是本機上Microsoft Office 2000的安裝目錄,可根據個人機器上的實際安裝目錄修改)。選擇_Application、Workbooks、_Workbook、 Worksheets、_Worksheet、Range,加入新類,分別為_Application、Workbooks、_Workbook、 Worksheets、_Worksheet、Range,頭文件Excel9.h,源文件Excel9.cpp。
在ExcelTestDlg.cpp文件的頭部,#include "ExcelTestDlg.h"語句之下,增加 :
#include "comdef.h"
#include "Excel9.h"
在void CExcelTestDlg::OnExceltest() 函數中增加如下代碼:
void CExcelTestDlg::OnExceltest()
{
_Application ExcelApp;
Workbooks wbsMyBooks;
_Workbook wbMyBook;
Worksheets wssMysheets;
_Worksheet wsMysheet;
Range rgMyRge;
//創建Excel 2000伺服器(啟動Excel)

if (!ExcelApp.CreateDispatch("Excel.Application",NULL))
{
AfxMessageBox("創建Excel服務失敗!");
exit(1);
}
//利用模板文件建立新文檔
wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks(),true);
wbMyBook.AttachDispatch(wbsMyBooks.Add(_variant_t("g:\\exceltest\\MyTemplate.xlt")));
//得到Worksheets
wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(),true);
//得到sheet1
wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("sheet1")),true);
//得到全部Cells,此時,rgMyRge是cells的集合
rgMyRge.AttachDispatch(wsMysheet.GetCells(),true);
//設置1行1列的單元的值
rgMyRge.SetItem(_variant_t((long)1),_variant_t((long)1),_variant_t("This Is A Excel Test Program!"));
//得到所有的列
rgMyRge.AttachDispatch(wsMysheet.GetColumns(),true);
//得到第一列
rgMyRge.AttachDispatch(rgMyRge.GetItem(_variant_t((long)1),vtMissing).pdispVal,true);
//設置列寬
rgMyRge.SetColumnWidth(_variant_t((long)200));
//調用模板中預先存放的宏
ExcelApp.Run(_variant_t("CopyRow"),_variant_t((long)10),vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,
vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing);
//列印預覽
wbMyBook.SetSaved(true);
ExcelApp.SetVisible(true);
wbMyBook.PrintPreview(_variant_t(false));
//釋放對象
rgMyRge.ReleaseDispatch();
wsMysheet.ReleaseDispatch();
wssMysheets.ReleaseDispatch();
wbMyBook.ReleaseDispatch();
wbsMyBooks.ReleaseDispatch();
ExcelApp.ReleaseDispatch();
}

2. 如何使用Python3讀寫INI配置文件

ini文件簡介
ini是我們常見到的配置文件格式之一。
ini是微軟Windows操作系統中的文件擴展名(也常用在其他系統)。
INI是英文「初始化(Initial)」的縮寫。正如該術語所表示的,INI文件被用來對操作系統或特定程序初始化或進行參數設置。
網路
通過它,可以將經常需要改變的參數保存起來(而且還可讀),使程序更加的靈活。
我先給出一個ini文件的示例。
[School]
ip = 10.15.40.123
mask = 255.255.255.0
gateway = 10.15.40.1
dns = 211.82.96.1

[Match]
ip = 172.17.29.120
mask = 255.255.255.0
gateway = 172.17.29.1
dns = 0.0.0.0

這個配置文件中保存的是不同場合下的IP設置參數。
下面將以生成和讀取這個配置文件為例,進行講解。
Python(v3)讀取方法
首先,Python讀取ini配置需要用到ConfigParser包,所以要先載入它。
import configparser

之後我們需要載入配置文件。
config=configparser.ConfigParser()

#IpConfig.ini可以是一個不存在的文件,意味著准備新建配置文件。
config.read("IpConfig.ini")

接下來,我們可以使用configparser.add_section()向配置文件中添加一個Section。
#添加節School
config.add_section("School")

注意:如果文件中已經存在相應的項目,則不能再增加同名的節。
然後可以使用configparser.set()在節School中增加新的參數。
#添加新的IP地址參數
config.set("School","IP","192.168.1.120")
config.set("School","Mask","255.255.255.0")
config.set("School","Gateway","192.168.1.1")
config.set("School","DNS","211.82.96.1")

你可以以同樣的方式增加其它幾項。
#由於ini文件中可能有同名項,所以做了異常處理
try:
config.add_section("Match")
config.set("Match","IP","172.17.29.120")
config.set("Match","Mask","255.255.255.0")
config.set("Match","Gateway","172.17.29.1")
config.set("Match","DNS","0.0.0.0")
except configparser.DuplicateSectionError:
print("Section 'Match' already exists")

增加完所有需要的項目後,要記得使用configparser.write()進行寫入操作。
config.write(open("IpConfig.ini", "w"))

以上就是寫入配置文件的過程。
接下來我們使用configparser.get()讀取剛才寫入配置文件中的參數。讀取之前要記得讀取ini文件。
ip=config.get("School","IP")
mask=config.get("School","mask")
gateway=config.get("School","Gateway")
dns=config.get("School","DNS")

print((ip,mask+"\n"+gateway,dns))

完整示例
下面是一個完整的示常式序,他將生成一個IpConfig.ini的配置文件,再讀取文件中的數據,輸出到屏幕上。
# -*- coding: utf-8 -*-

import configparser

#讀取配置文件
config=configparser.ConfigParser()
config.read("IpConfig.ini")

#寫入宿舍配置文件
try:
config.add_section("School")
config.set("School","IP","10.15.40.123")
config.set("School","Mask","255.255.255.0")
config.set("School","Gateway","10.15.40.1")
config.set("School","DNS","211.82.96.1")
except configparser.DuplicateSectionError:
print("Section 'School' already exists")

#寫入比賽配置文件
try:
config.add_section("Match")
config.set("Match","IP","172.17.29.120")
config.set("Match","Mask","255.255.255.0")
config.set("Match","Gateway","172.17.29.1")
config.set("Match","DNS","0.0.0.0")
except configparser.DuplicateSectionError:
print("Section 'Match' already exists")

#寫入配置文件
config.write(open("IpConfig.ini", "w"))

ip=config.get("School","IP")
mask=config.get("School","mask")
gateway=config.get("School","Gateway")
dns=config.get("School","DNS")

print((ip,mask+"\n"+gateway,dns))

總結
Python讀取ini文件還是十分簡單的,這里我給出的只是一些簡單的使用方法,如果想用更高級的功能,比如和注釋有關的功能。可以參考Pyhton官方文檔

3. 請問在VB6.0中,怎樣讀寫和生成.ini配置文件

給你一個類,專門讀寫INI的

classIniFile.cls:

Option Explicit

Private strINI As String
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Function MakePath(ByVal strDrv As String, ByVal strDir As String) As String
Do While Right$(strDrv, 1) = "\"
strDrv = Left$(strDrv, Len(strDrv) - 1)
Loop

Do While Left$(strDir, 1) = "\"
strDir = Mid$(strDir, 2)
Loop

MakePath = strDrv & "\" & strDir
End Function

Private Sub CreateIni(strDrv As String, strDir As String)
strINI = MakePath(strDrv, strDir)
End Sub

Public Sub WriteIniKey(strSection As String, strKey As String, strValue As String)
WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub

Public Function GetIniKey(strSection As String, strKey As String) As String
Dim strTmp As String
Dim lngRet As String
Dim I As Integer
Dim strTmp2 As String

strTmp = String$(1024, Chr(32))
lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strINI)
strTmp = Trim(strTmp)
strTmp2 = ""
For I = 1 To Len(strTmp)
If Asc(Mid(strTmp, I, 1)) <> 0 Then
strTmp2 = strTmp2 + Mid(strTmp, I, 1)
End If
Next I
strTmp = strTmp2

GetIniKey = strTmp
End Function

Public Property Let INIFileName(ByVal New_IniPath As String)
strINI = New_IniPath
End Property

Public Property Get INIFileName() As String
INIFileName = strINI
End Property

Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
End Function

Public Function DelIniSec(ByVal SectionName As String)
Dim RetVal As Integer
RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
End Function

示例:

Dim myIni As New classIniFile
Const myinifile = "\mail.ini"
myIni.INIFileName = App.Path & myinifile
myIni.WriteIniKey "mail", "pop3", pop3

4. 在Linux環境下,對一個設備文件進行多線程讀寫(兩個線程就行),求大神給一個簡單的程序。

配置文件為 conf.txt
測試代碼如下,注意鏈接的時候加上 -lpthread 這個參數

#include <stdio.h>
#include <errno.h> //perror()
#include <pthread.h>

#include <unistd.h> //sleep()
#include <time.h> // time()
#include <stdlib.h> //rand()

#define FD "conf.txt"

typedef void *(*fun)(void *);

struct my_struct
{
unsigned time_to_wait;
int n;
};

void *test_thread(struct my_struct *);

int main (int argc, char const *argv[])
{
FILE *fp = fopen(FD, "r");
if (fp == NULL)
{
perror(FD);
return -1;
}

srand((unsigned)time(NULL)); //初始化隨機種子

int thread_count;
fscanf(fp, "%d", &thread_count);
fclose(fp);

if (thread_count <= 0)
{
printf("線程數<1,退出程序。\n");
return -1;
}

pthread_t *ptid = (pthread_t *)malloc(sizeof(pthread_t) * thread_count); //保存線程ID

int i;
for (i = 0; i < thread_count; i++)
{
int tw = rand() % thread_count + 1; //隨機等待時間

struct my_struct * p = (struct my_struct *)malloc(sizeof(struct my_struct));
if (p == NULL)
{
perror("內存分配錯誤");
goto ERROR;
}
p->time_to_wait = tw;
p->n = i + 1;

int rval = pthread_create(ptid + i, NULL, (fun) test_thread, (void *)(p)); //注意這里的強制轉換(兩個)
if (rval != 0)
{
perror("Thread creation failed");
goto ERROR;
}
//sleep(1); //這句加也可以,不加也可以。最開始的時候加上這個是為了讓兩個線程啟動的時候之間有一定的時間差
}

printf("主線程啟動\n\n");
fflush(stdout);
for (i = 0; i < thread_count; i++)
{
pthread_join(*(ptid + i), NULL); //等待所有線程退出。
}
printf("\n主線程退出\n");
ERROR:
free(ptid);
return 0;
}

void *test_thread(struct my_struct * p) //線程啟動的時候運行的函數
{
printf("第%d個線程啟動,預計運行%d秒\n", p->n, p->time_to_wait);
fflush(stdout);

sleep(p->time_to_wait); //讓線程等待一段時間
printf("第%d個線程結束\n", p->n);
fflush(stdout);
free(p);
return NULL;
}

你的第二個問題我在網路HI回你了~

閱讀全文

與配置文件讀寫的案例相關的資料

熱點內容
note3每次開機都要輸wifi密碼 瀏覽:590
網路只能聊天不能看視頻怎麼辦 瀏覽:544
金融和大數據哪個難度高 瀏覽:613
蘋果手機文件功能沒了 瀏覽:805
vuejs滾動到底部 瀏覽:66
哈爾濱少兒編程哪個機構好 瀏覽:881
蘋果賬戶怎麼登陸app 瀏覽:807
el表達式教程 瀏覽:710
js評價模板 瀏覽:91
如何將數據按遞增順序排序 瀏覽:769
為什麼資料庫在海底 瀏覽:260
jsp不允許輸入空格 瀏覽:275
amd文件夾佔用多少g 瀏覽:310
鏈接怎麼在微信里查看密碼 瀏覽:827
蘋果5描述文件找不到 瀏覽:927
qq發送文件不可以超過多少g 瀏覽:491
配置文件讀寫的案例 瀏覽:538
中指符號表情代碼emoji 瀏覽:170
i5用什麼軟體編程 瀏覽:496
編程看不懂英文哪些翻譯軟體 瀏覽:282

友情鏈接