1. 程式人生 > >去掉txt檔案中每一行字串內容的第一個空格

去掉txt檔案中每一行字串內容的第一個空格

demo的作用

  • 在opencv訓練分類器的時候,前期製作樣本的時候,師弟們把樣本的名字中帶有了空格,但是訓練的時候樣本的名字是不能帶有空格的。而樣本的名字儲存在一個txt資料夾中,所以需要修改txt檔案中的樣本名字。
  • 讀寫檔案
  • find字串的空格位置
  • 查詢空格
  • 刪除空格

程式碼展示

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
/////得到一個字串的空格數量
int get_space_number_in_string(string
str); /////判斷一個字串中是否有空格 bool is_have_space_in_string(string str); //////得到一個字串的第一個空格的位置 int get_first_space_pos(string& str); ////刪除pos位置上的字元 bool is_delete_pos_in_string(string& str,string::size_type pos); int main() { string my_str = "ninpi hasdhh asjdbjb iiasgdj hoh sdad"; //string::size_type pos = my_str.find(" ");
//cout<<"pos = "<<pos<<endl; //my_str.erase(5,1); ifstream in_file; ofstream out_file; in_file.open("info1_400.txt"); out_file.open("out_file_info1_400.txt"); if(!in_file.is_open()) { cout<<"file is not opened "<<endl; return -1; } else
{ cout<<"file is opened"<<endl; cout<<"開始讀取檔案......"<<endl; string str_line; while(getline(in_file,str_line)) { if(get_space_number_in_string(str_line) == 6) { int first_space_pos = get_first_space_pos(str_line); if(is_delete_pos_in_string(str_line,first_space_pos)) { cout<<"此時刪除了第一個空格"<<endl; cout<<"此時,這行的內容變成"<<str_line<<endl; } } out_file<<str_line<<endl; } } in_file.close(); out_file.close(); system("pause"); return 0; } /////得到一個字串的空格數量 int get_space_number_in_string(string str) { string::size_type pos = 0; int space_count = 0; while((pos = (str.find_first_of(" ",pos))) != string::npos) { pos++; space_count++; } //cout<<"字串的空格數量:"<<space_count<<endl; return space_count; } /////判斷一個字串中是否有空格 bool is_have_space_in_string(string str) { string::size_type pos = str.find(" "); if(pos == string::npos) return false; else return true; } //////得到一個字串的第一個空格的位置 int get_first_space_pos(string& str) { string::size_type pos = 0; pos = str.find_first_of(" ",pos); return pos; } ////刪除pos位置上的字元 bool is_delete_pos_in_string(string& str,string::size_type pos) { if(pos >= string::npos) return false; str.erase(pos,1); return true; }

相關推薦

去掉txt檔案一行字串內容第一空格

demo的作用 在opencv訓練分類器的時候,前期製作樣本的時候,師弟們把樣本的名字中帶有了空格,但是訓練的時候樣本的名字是不能帶有空格的。而樣本的名字儲存在一個txt資料夾中,所以需要修改txt檔案中的樣本名字。 讀寫檔案 find字串的空格位置 查詢空

java——修改txt檔案一行內容

  今天無意間看到java.io中有一個類:RandomAccessFile,可以在檔案的任意位置進行讀寫操作,想到我之前寫的一個小專案,想在txt中修改某一行的內容,都是從頭遍歷txt檔案,修改這一行的同時,把整個檔案放入快取,再把修改後的內容重新寫入檔案,可以說是很蠢了,但一直沒找到好的辦法。   這個

Java統計一個文字檔案一行字串出現的次數

需求:現要統計一個文字檔案中每一行字串出現次數 public static HashMap<String, Integer> analysizeFile(File file) { Ha

【C語言】向建立的 d:\\demo.txt 檔案追加一個字串

#include<stdio.h> int main() { FILE *fp; char str[102] = { 0 }, strTemp[100]; if ((fp = fopen("D:\\demo.txt", "at+")) == NULL) {

C語言在TXT檔案搜索字串

方法一: bool CBrowseDir::SearchTXT(const char *filename) { string filename1( filename ); string keyword( "word" ); ifs

找出陣列個數的右邊第一比它大的數

#include <bits/stdc++.h> using namespace std; vector<int> findMax(vector<int>num) { if(num.size()==0)return num;

php遍歷文字文件txt檔案的連結內容為陣列

1 <?php 2 3 $file = fopen("1.txt", "r"); 4 $user=array(); 5 $i=0; 6 //輸出文字中所有的行,直到檔案結束為止。 7 while(! feof($file)) 8 { 9 $user[$i]= fgets($fi

讓隱私內容存在txt檔案

實驗的目的,純粹是為了好玩,知道有這麼一項功能,裝逼用的哈哈。 在D:盤上建立一個有內容的txt檔案my.txt,內容如下: 使用命令,切換到D:盤,並敲入如下命令: C:\Users\yangfeng>cd /d D: D:\>notepad my.txt:som

pythonstr與list互換,txt檔案的讀取,字串變成列表操作,另存為TXT時從列表變成字串

file = open ("wider_face_train_bbx_gt.txt") for line in lines: print(type(line)) #<type 'str'> #78 221 7 8 2 0 0 0 0

java的io操作(將字串寫入到txt檔案)

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOEx

在D盤中建立檔案test.txt,檔案內容為:hello Java,然後利用流把該檔案拷貝到E盤根目錄

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream

java-讀取txt檔案的第一行URL並進行請求,得出響應

背景:其實我是拿來練手的。想寫程式碼而已,並沒有啥特殊的目地 我的需求: 有一個url.txt檔案,裡面裝的一行一行的URL 我要請求每一行URL,然後去請求,拿到第一個請求的響應結果,程式碼很簡單,網上一搜遍地都是,我加工了一下。 整合了 直接上碼吧! p

Python開啟檔案,將list、numpy陣列內容寫入txt檔案

python儲存numpy資料:numpy.savetxt("result.txt", numpy_data); 儲存list資料:file=open('data.txt','w') file.wri

php 讀取txt檔案內容,轉換成陣列

$str = file_get_contents('weixinname.txt');//將整個檔案內容讀入到一個字串中 $str_encoding = mb_convert_encod

編寫程式,完成檔案複製功能,即將a.txt檔案內容複製到b.txt檔案

import java.io.*; public class copyfile { /** * @param args */ public static void main(String[] args) { String oldPath="c://a.txt"

python在TXT檔案按照某一字串取出該字串所在的行

主要流程:讀取檔案資料——將每一行資料分成不同的字元段——在判斷 在某個字否段是否含與某個字元。(只是其中一種辦法)程式碼如下:with open(r"C:\Users\LENOVO\Desktop\20170513155231.txt", encoding='utf

python操作txt檔案資料教程[4]-python去掉txt檔案行尾換行

python操作txt檔案中資料教程[4]-python去掉txt檔案行尾換行 覺得有用的話,歡迎一起討論相互學習~Follow Me 參考文章 python操作txt檔案中資料教程[1]-使用python讀寫txt檔案 python操作txt檔案中資料教程[2]-python提取txt檔案中的行列

關於javascript從txt檔案讀取內容出現亂碼的問題

在開啟檔案時,設為-2或者0,而不是不設或者設為-1。(有語義和下面的衝突) OpenTextFile 方法 開啟指定的檔案並返回一個 TextStream 物件,可以讀取、寫入此物件或將其追加到檔案。 object.OpenTextFile(filename[,

python 將print輸出的內容儲存到txt檔案

import sys import os class Logger(object): def __init__(self, filename="Default.log"): self.terminal = sys.stdout sel

c語言隔1秒向檔案寫入一行記錄,類似日誌記錄

讀寫一個檔案test.txt,每隔1秒向檔案中寫入一行記錄,直到按Ctrl-C終止。下次啟動程式時在test.txt檔案末尾追加記錄,並且序號能夠接續上次的序號。 #include <stdio