读写文件

1、c++读写,生成json字符串

void main(int argc, char **argv)
{
	string path = "C:\\new\\";
	ifstream in("C:\\new\\new_testResult.txt", ios::in);
	bool flag = true;

	Json::Value root;
	Json::Value result;
	Json::Value value;
	string preWord;

	for(string line; getline(in,line);)
	{
		string word;		
		string title;
		string url;

		int title_index = line.find("\t");
		if( title_index != string::npos)
		{
			title = line.substr(0, title_index);
		}

		line = line.substr(title_index + 1);

		int word_index = line.find("\t");
		if( word_index != string::npos)
		{
			word = line.substr(0, word_index);
		}

		url = line.substr(word_index + 1);

		if( flag)
		{
			AssignmentVlaue( url, title, value, result);
			flag = false;
			preWord = word;
			continue;
		}
		if( word == preWord )
		{
			value.clear();
			AssignmentVlaue( url, title, value, result);
			preWord = word;
			continue;
		}
		
		root["index_version"] = "";
		root["gas_version"] = "0.8.0";
		root["result"] = result;
		Json::FastWriter write;

		ofstream fwrite( path  + preWord + ".txt", ios::out);//没有文件会创建相关文件

		string jsonStr = write.write( root );
		char *p = const_cast<char *>( jsonStr.c_str() );
		fwrite<<jsonStr<<endl;
		fwrite.close();

		root.clear();
		value.clear();
		result.clear();

		AssignmentVlaue( url, title, value, result);
		preWord = word;
	}
}

  

C++读写二进制文件了。
要读取文件必须包含<fstream>头文件,这里包含了C++读写文件的方法,可以使用fstream 类,这个类可以对文件进行读写操作。
 
1、打开文件。
 
打开文件可以有两种方式,第一种可以使用fstream 类的构造函数。
ios::in :输入文件(同ios_base::in)
ios::out :输出文件(同ios_base::out)
ios::app :追加方式(同ios_base::app)
ios::binary:二进制形式(同ios_base::binary)
 
可以通过 “|” 匹配多个,如:
 
fstream file("test.dat",ios::in|ios::binary); //以二进制形式读
 
另外一种方法就是使用open 函数。
 
fstream file;
file.open("test.dat",ios_base::in|ios_base::out|ios_base::app);
 
这样就可以打开一个可读写的文件了。如果文件不存在的话,就会创建一个新文件并且以读写方式打开。
注意:这里需要说明一点,如果文件不存在的话,open 函数中第二个参数必须包含ios_base::out|ios_base::app,否则就不能正确创建文件。
 
2、写文件。
 
既然是写二进制文件可以向文件中写入一个整形值。写二进制字符只能使用write 函数。
但是write 函数的原形是write(const char * ch, int size)。第一个参数是char *类型,所以需要把将要写入
文件的int 类型转换成char *类型。这里的转换困扰了我好几天,不过终于弄明白了。代码如下。
int temp;
file.write((char *)(&temp),sizeof(temp));
 
必须转换为 char*。
 
数组可以一次写入,没有必要逐个写(其中数组中类型可以是结构体什么的,只要里面不含有指针);
 
3、读文件。
 
可以写文件了,读文件就好办多了。读文件需要用到read 函数。其参数和write 大致相同,read(const char * ch, int size)。
要把内容读到int 类型变量中同样涉及到一个类型转换的问题。和写文件一样。
int readInt;
file.read((char *)(&readInt),sizeof(readInt));
这样文件中的int 值就读入到int 型变量readInt 中了。
数组可以一次读入,没有必要逐个读(其中数组中类型可以是结构体什么的,只要里面不含有指针);

2、python

from urllib2 import Request, urlopen, URLError, HTTPError

urls = open('caipu_processed_tempResult.txt', 'rb')
fwrite = open('caipu_processed_tempResult_result.txt', 'wb')

for url in urls:
	segs = url.split('\t')
	user_agent = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19'
	headers = { 'User-Agent' : user_agent }
	if len(segs) > 2:
		req = Request(segs[2])
		try:response = urlopen(req)
		except URLError, e:
			continue
		else:
			continue
		fwrite.write(segs[0])
		fwrite.write('\t')
		fwrite.write(segs[1])
		fwrite.write('\t')
		fwrite.write(response.geturl())
		fwrite.write('\r\n')
urls.close()
fwrite.close()

http://www.cnblogs.com/greatverve/archive/2012/10/29/cpp-io-binary.html

3、C#读写

StreamReader sr = new StreamReader("E:\\工作目录\\20140313\\li\\liyang.txt", false);
  while (!sr.EndOfStream)
 {
        string line = sr.ReadLine();
       ----
}
sr.Close();

FileStream fs = new FileStream("E:\\工作目录\\20140313\\li\\testResult_category.txt", FileMode.Create, FileAccess.Write);
fs.Close();
StreamWriter sw = new StreamWriter("E:\\工作目录\\20140313\\li\\testResult_category.txt");
//StreamWriter sw_temp = new StreamWriter("E:\\工作目录\\百度抓取结果\\new\\testResult_" + myDictionary[segments[0]] + ".txt", true); 以追加的方式打开 sw.WriteLine(-----

posted on 2014-04-23 09:34  雪 狼  阅读(217)  评论(0编辑  收藏  举报

导航