Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c++; 我想用C++来更新我的TXT文件中的数据,这是解释: txt文件的格式如下: khanh 123456 0 lien 124123 1 hahaha 125225 0_C++_File_Updates - Fatal编程技术网

如何使用c++; 我想用C++来更新我的TXT文件中的数据,这是解释: txt文件的格式如下: khanh 123456 0 lien 124123 1 hahaha 125225 0

如何使用c++; 我想用C++来更新我的TXT文件中的数据,这是解释: txt文件的格式如下: khanh 123456 0 lien 124123 1 hahaha 125225 0,c++,file,updates,C++,File,Updates,每行代表一个帐户,第一个字是用户名,最后一个字是密码,最后一个是该帐户的状态(0是活动,1是阻止)。例:第2行:用户名是lien,密码是124123,状态是1 我想根据一个账户的用户名更新该账户的状态。 例如:当字符串(或数组字符)包含字符串hahaha(例如:string=“hahaha”或char*arry=“hahaha”) 我怎么做,我找到了一些解决方案,它只覆盖了txt文件中的第一行,有人可以帮助我,如何像这样更新.txt文件中的字符 请注意,您不能覆盖该文件。另一种解决方案是只获取指

每行代表一个帐户,第一个字是用户名,最后一个字是密码,最后一个是该帐户的状态(0是活动,1是阻止)。例:第2行:用户名是
lien
,密码是
124123
,状态是
1

我想根据一个账户的用户名更新该账户的状态。
例如:当字符串(或数组字符)包含字符串
hahaha
(例如:string=“hahaha”或char*arry=“hahaha”)

我怎么做,我找到了一些解决方案,它只覆盖了txt文件中的第一行,有人可以帮助我,如何像这样更新.txt文件中的字符

请注意,您不能覆盖该文件。另一种解决方案是只获取指定格式的数据,并生成编辑版本的输出。这是我为您的需求编写的代码:

#include <iostream>
#include <fstream>

int main(void)
{
    std::ifstream file;
    std::ofstream out;

    std::string str;
    short int status; // status = 0, 1
    int bal, i = 1;

    file.open("test.txt"); // opening test.txt
    out.open("output.txt"); // opening output.txt here, otherwise it'll iterate in loop

    // gets input in this format
    while (file >> str >> bal >> status)
    {
        std::cout << "[" << i << "] " << str << ' ' << bal << ' ' << status << std::endl;

        std::cout << "Account status for this person (1 or 0): ";
        std::cin >> status;
        out << str << ' ' << bal << ' ' << status << std::endl;

        i++;
    }

    file.close();
    out.close();

    return 0;
}
然后,您应该在
output.txt
中获得此信息:

khanh 123456 1
lien 124123 0
hahaha 125225 0

享受吧

您可以在读写模式下打开文件。请记住,如果要更新长度不同于旧值的值,则需要移动下面的所有数据,或者只需添加空格以适应固定长度

#include <stdio.h>
#include <string.h>

char name[]="hahaha";
char newvalue='1';

int main(void)
{
   FILE* filePtr = fopen("try.txt", "r+");
   char c;
   bool isvalidname=true;
   int name_idx=0;
   while((c = fgetc(filePtr)) != EOF)
   {
       if(c == '\n')
       {
           isvalidname=true;
       }
       if(c == '\r' || c=='\n')
       {
           continue;
       }

       if( isvalidname && ( name_idx + 1 == strlen(name)))
       {
           isvalidname = false;
       }

       if( isvalidname && (name_idx< strlen(name) && name[name_idx]!=c))
       {
           isvalidname = false;
       }
       if(isvalidname)
       {
           name_idx++;
       }

       if(isvalidname && (name_idx == strlen(name) -1))
       {
           int spaces=0;
           while((c = fgetc(filePtr)) != EOF)
           {
                if(c==' ') spaces++;
                if(spaces==2) 
                {
                    fseek(filePtr, 0, SEEK_CUR);
                    fputc(newvalue, filePtr);
                    break;
                }
           }
       }

   }
   fclose(filePtr);
   return 0;
}
#包括
#包括
字符名[]=“哈哈哈”;
char newvalue='1';
内部主(空)
{
FILE*filePtr=fopen(“try.txt”、“r+”);
字符c;
bool isvalidname=true;
int name_idx=0;
而((c=fgetc(filePtr))!=EOF)
{
如果(c=='\n')
{
isvalidname=true;
}
如果(c='\r'| c=='\n')
{
继续;
}
如果(isvalidname&(name_idx+1==strlen(name)))
{
isvalidname=false;
}
if(isvalidname&(name_idx
这是我的简短代码,使用
查看kp
tellg

#包括
#包括
#包括
int main(){
std::fstream文件(“data.txt”,std::ios::in | std::ios::out);
std::字符串用户、传递、查询;
字符状态;
std::cin>>query;//输入要切换其状态的用户
while(文件>>用户>>通行证)
如果(用户==查询){
auto pos=file.tellg();
文件>>状态;
文件。seekp(pos);
文件>状态;
}
file.close();
返回0;
}

无法覆盖文本文件中的数据。您需要做的是将整个文件读入内存,在内存中进行所需的更改,然后将整个文件写入磁盘。@john只有在更改可变长度字段时,您的语句才是正确的(我的意思是,当一个人想要改变某个东西,而它的新值将改变存储该特定部分所需的空间时,这个术语非常模糊)。在这里,询问者只想改变保证为单个字符的状态(
0
1
)。他只需在读取状态之前存储
streampos
,并在查找到存储位置后打印新状态。他实际上不需要读取整个文件。
#include <stdio.h>
#include <string.h>

char name[]="hahaha";
char newvalue='1';

int main(void)
{
   FILE* filePtr = fopen("try.txt", "r+");
   char c;
   bool isvalidname=true;
   int name_idx=0;
   while((c = fgetc(filePtr)) != EOF)
   {
       if(c == '\n')
       {
           isvalidname=true;
       }
       if(c == '\r' || c=='\n')
       {
           continue;
       }

       if( isvalidname && ( name_idx + 1 == strlen(name)))
       {
           isvalidname = false;
       }

       if( isvalidname && (name_idx< strlen(name) && name[name_idx]!=c))
       {
           isvalidname = false;
       }
       if(isvalidname)
       {
           name_idx++;
       }

       if(isvalidname && (name_idx == strlen(name) -1))
       {
           int spaces=0;
           while((c = fgetc(filePtr)) != EOF)
           {
                if(c==' ') spaces++;
                if(spaces==2) 
                {
                    fseek(filePtr, 0, SEEK_CUR);
                    fputc(newvalue, filePtr);
                    break;
                }
           }
       }

   }
   fclose(filePtr);
   return 0;
}