0

## 藍芽接收並執行動作

Hwr並接收此値，假設接收的値都為1~10

int main(void) {
int a; //bluetooth
int Hwr;
int Ctr;
int  Lwr[6];
while (1) {
if (a == 1) {

Hwr =a;
if (Hwr <= 6){
FILE *Lwr[6];
Lwr[6] = fopen("test.txt", "r");//讀檔
Ctr = Lwr[Hwr];
}
else{
Ctr = Hwr;
}
}
switch(ctr){
case 97;
keybd_event(xxxx);
}
}


### 2 個回答

0

iT邦大神 1 級 ‧ 2017-11-21 08:16:30

if (a == 1) {
Hwr =a;

0
if (Hwr <= 6){
FILE *Lwr[6];
Lwr[6] = fopen("test.txt", "r");//讀檔
Ctr = Lwr[Hwr];
}


Lwr[Hwr] 所以這個地方取出來的型態是 FILE *，您將其賦予給 int Ctr，型態不符所以出錯了，

pigs0231 iT邦新手 5 級 ‧ 2017-11-22 00:57:45 檢舉

，但是查詢的結果不是讀檔所讀取出來的値

if (serial.Open(4, 9600)){
printf("Port opened successfully\n");
char *lpBuffer =(char*)malloc(sizeof(char)*50);

while(1){
int Hwr,ctr;
int h[6];

FILE *Lwr;
if (Hwr <=6)
{
Lwr = fopen("C:\\Users\\user\\Desktop\\BlueTooth(2)\\text.txt","r");
for (int i = 0; i < 6; i++)//
{
fscanf(Lwr,"%d",h[i]);
}
ctr = h[Hwr];
printf("%d\n",lpBuffer);
fclose(Lwr);
}else
{
ctr = Hwr;
}
}


test.txt 的內容是什麼呢?

pigs0231 iT邦新手 5 級 ‧ 2017-11-23 00:40:15 檢舉

test主要是網頁所讀取出來的圖片編號，切換時會將編號重新寫檔

1.讀檔時，所讀取出來的會出現亂碼
2.只能讀取一次test.txt檔案的樣子，如果切換網頁編號會改變，但是如果讀取只能讀取一次，這樣就沒辦法利用switch case在利用keybd-event來操作了。

int main(){
if (serial.Open(4, 9600)){
printf("Port opened successfully\n");
char *lpBuffer =(char*)malloc(sizeof(char)*50);

while(1){
int ctr;
int h	[6];

if (Hwr <=6)
{
FILE* Lwr;

fopen_s(&Lwr,"C:\\Users\\user\\Desktop\\BlueTooth(2)\\test.txt","r");

for (int i = 0; i < 6; i++)
{
fscanf(Lwr,"%d",&h[i]);
}
ctr = h[Hwr];
printf("%d\n",lpBuffer);
fclose(Lwr);
}else
{
ctr = Hwr;
}


#include <stdio.h>
#include <stdlib.h>

int main(void)
{
while (true)
{
int data[5] = {0};

FILE *pFile = fopen("test.txt", "r");

for (int i = 0; i < 5; i++)
{
fscanf(pFile, "%d", &data[i]);
}
fclose(pFile);

int stop = 0;
}

system("pause");
return 0;
}


text.txt 內容

1 2 3 4 5