练习8.1:编写函数。接受一个istream&参数,返回值类型也是istream&。此函数必须从给定流中读取数据,直至遇到文件结束标识时停止。

  1. #include <iostream>
  2. #include <stdexcept>
  3. using std::istream;
  4. using std::cin;
  5. using std::cout;
  6. using std::cerr;
  7. using std::endl;
  8. using std::runtime_error;
  9. istream &f(istream &in)
  10. {
  11. int v;
  12. while(in >> v, !in.eof())
  13. {
  14. if(in.bad())
  15. throw runtime_error("IO Stream error.");
  16. if(in.fail())
  17. {
  18. cerr<<"Data error! Please try again."<<endl;
  19. in.clear();
  20. in.ignore(100, '\n');
  21. continue;
  22. }
  23. cout<< v <<endl;
  24. }
  25. in.clear();
  26. return in;
  27. }
  28. int main()
  29. {
  30. cout<<"Please input some numbers, enter Ctrl+Z to end"<<endl;
  31. f(cin);
  32. return 0;
  33. }

练习8.3:什么情况下,下面的while循环会终止?

while (cin >> i) /*. . .*/

遇到文件结束符,或者遇到了IO流错误或者读入了无效数据。

练习8.4:编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中。

    #include <iostream>
#include <string>
#include <fstream>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::cerr;
using std::vector; int main()
{
ifstream in("data.txt");
if(!in) {
cerr<<"Can't open the file."<<endl;
return -1;
} string line;
vector<string> words;
while(getline(in, line))
words.push_back(line); in.close(); vector<string>::const_iterator it = words.cbegin();
while (it != words.cend())
{
cout<< *it <<endl;
++it;
}
return 0;
}

练习8.5:重写上面的程序,将每个单词作为一个独立的元素进行存储。

    #include <iostream>
#include <string>
#include <fstream>
#include <vector>
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::cerr;
using std::vector; int main()
{
ifstream in("data.txt");
if(!in) {
cerr<<"Can't open the file."<<endl;
return -1;
} string line;
vector<string> words;
while(in >> line)
words.push_back(line); in.close(); vector<string>::const_iterator it = words.cbegin();
while (it != words.cend())
{
cout<< *it <<endl;
++it;
}
return 0;
}

练习8.6:重写7.1.1节的书店程序,从一个文件中读取交易记录。将文件名作为一个参数传递给main。

  1. #include <iostream>
  2. #include <fstream>
  3. #include "Sales_data.h"
  4. using std::cout;
  5. using std::cerr;
  6. using std::ifstream;
  7. using std::endl;
  8. int main(int argc, char *argv[])
  9. {
  10. if (argc != 2) {
  11. cerr<< "Please give the file name."<<endl;
  12. return -1;
  13. }
  14. ifstream in(argv[1]);
  15. if (!in) {
  16. cerr<<"Can't open the file."<<endl;
  17. return -1;
  18. }
  19. Sales_data total;
  20. if (read(in, total)) {
  21. Sales_data trans;
  22. while(read(in ,trans)) {
  23. if(total.isbn() == trans.isbn())
  24. total.combine(trans);
  25. else {
  26. print(cout, total) << endl;
  27. total =trans;
  28. }
  29. }
  30. print(cout, total)<<endl;
  31. }
  32. else {
  33. cerr<<" No data?!"<<endl;
  34. }
  35. return 0;
  36. }

练习8.7:修改上一节的书店程序,将结果保存到一个文件中。将输出文件名作为第二个参数传递给main函数。

  1. #include <iostream>
  2. #include <fstream>
  3. #include "Sales_data.h"
  4. using std::cerr;
  5. using std::ifstream;
  6. using std::ofstream;
  7. using std::endl;
  8. int main(int argc, char *argv[])
  9. {
  10. if (argc != 3) {
  11. cerr<< "Please give the input file name and out file name."<<endl;
  12. return -1;
  13. }
  14. ifstream in(argv[1]);
  15. if (!in) {
  16. cerr<<"Can't open the file."<<endl;
  17. return -1;
  18. }
  19. ofstream out(argv[2]);
  20. if (!out) {
  21. cerr<<"can't open output file."<<endl;
  22. return -1;
  23. }
  24. Sales_data total;
  25. if (read(in, total)) {
  26. Sales_data trans;
  27. while(read(in ,trans)) {
  28. if(total.isbn() == trans.isbn())
  29. total.combine(trans);
  30. else {
  31. print(out, total) << endl;
  32. total =trans;
  33. }
  34. }
  35. print(out, total)<<endl;
  36. }
  37. else {
  38. cerr<<" No data?!"<<endl;
  39. }
  40. return 0;
  41. }

练习8.8:修改上一题的程序,将结果追加到给定的文件末尾。对同一个输出文件,运行程序至少两次,检验数据是否得以保留。

  1. #include <iostream>
  2. #include <fstream>
  3. #include "Sales_data.h"
  4. using std::cerr;
  5. using std::ifstream;
  6. using std::ofstream;
  7. using std::endl;
  8. int main(int argc, char *argv[])
  9. {
  10. if (argc != 3) {
  11. cerr<< "Please give the input file name and out file name."<<endl;
  12. return -1;
  13. }
  14. ifstream in(argv[1]);
  15. if (!in) {
  16. cerr<<"Can't open the file."<<endl;
  17. return -1;
  18. }
  19. ofstream out(argv[2], ofstream::app);
  20. if (!out) {
  21. cerr<<"can't open output file."<<endl;
  22. return -1;
  23. }
  24. Sales_data total;
  25. if (read(in, total)) {
  26. Sales_data trans;
  27. while(read(in ,trans)) {
  28. if(total.isbn() == trans.isbn())
  29. total.combine(trans);
  30. else {
  31. print(out, total) << endl;
  32. total =trans;
  33. }
  34. }
  35. print(out, total)<<endl;
  36. }
  37. else {
  38. cerr<<" No data?!"<<endl;
  39. }
  40. return 0;
  41. }

练习8.3.1:使用你为8.1.2节第一个练习所编写的函数打印一个istringstream对象的内容。

  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <stdexcept>
  5. using std::istream;
  6. using std::ostringstream;
  7. using std::istringstream;
  8. using std::string;
  9. using std::cout;
  10. using std::cerr;
  11. using std::endl;
  12. using std::runtime_error;
  13. istream &f(istream &in)
  14. {
  15. int v;
  16. while(in >> v, !in.eof())
  17. {
  18. if(in.bad())
  19. throw runtime_error("IO Stream error.");
  20. if(in.fail())
  21. {
  22. cerr<<"Data error! Please try again."<<endl;
  23. in.clear();
  24. in.ignore(100, '\n');
  25. continue;
  26. }
  27. cout<< v <<endl;
  28. }
  29. in.clear();
  30. return in;
  31. }
  32. int main()
  33. {
  34. ostringstream msg;
  35. msg<<"C++ Primer 5th edition"<<endl;
  36. istringstream in(msg.str());
  37. f(in);
  38. return 0;
  39. }

练习8.10:编写程序,将来自一个文件中的行保存在一个vector<string>中。然后使用一个istringstream从vector读取数据元素,每次读取一个单词。

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. using std::cerr;
  7. using std::endl;
  8. using std::cout;
  9. using std::ifstream;
  10. using std::istringstream;
  11. using std::string;
  12. using std::vector;
  13. int main()
  14. {
  15. ifstream in("Data.txt");
  16. if (!in) {
  17. cerr<<" Can't open input file."<<endl;
  18. return -1;
  19. }
  20. string line;
  21. vector<string> words;
  22. while (getline(in, line)) {
  23. words.push_back(line);
  24. }
  25. in.close();
  26. vector<string>::const_iterator it = words.begin();
  27. while( it != words.end()) {
  28. istringstream line_str(*it);
  29. string word;
  30. while(line_str >> word)
  31. cout<< endl;
  32. ++it;
  33. }
  34. return 0;
  35. }

练习8.11:本节的程序在外层while循环中定义了istringstream对象。如果record对象定义在循环之外,你需要对程序进行怎么样的修改?重写程序,将record的定义移到while循环之外,验证你设想的修改方法是否正确。

  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5. using std::cin;
  6. using std::istringstream;
  7. using std::string;
  8. using std::vector;
  9. struct PersonInfo {
  10. string name;
  11. vector<string> phones;
  12. };
  13. int main()
  14. {
  15. string line, word;
  16. vector<PersonInfo> people;
  17. istringstream record;
  18. while (getline(cin,line)) {
  19. PersonInfo info;
  20. record.clear();
  21. record.str(line);
  22. record >> info.name;
  23. while (record >> word)
  24. info.phones.push_back(word);
  25. people.push_back(info);
  26. }
  27. return 0;
  28. }

练习8.12:我们为什么没有在PersonInfo中使用类内初始化?

由于每个人的电话号数量不固定,因此更好的方式不是通过类内初始化指定人名和所有电话号码,而是在缺省初始化之后,在程序中设置人名并逐个添加电话号码。

练习8.13:重写本节的电话号码程序,从一个命名文件而非cin读取数据。

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. using std::cerr;
  7. using std::endl;
  8. using std::cout;
  9. using std::ifstream;
  10. using std::istringstream;
  11. using std::ostringstream;
  12. using std::string;
  13. using std::vector;
  14. struct PersonInfo {
  15. string name;
  16. vector<string> phones;
  17. };
  18. string format(const string &s) { return s; }
  19. bool valid(const string &s)
  20. {
  21. return true;
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. string line, word;
  26. vector<PersonInfo> people;
  27. istringstream record;
  28. if (argc != 2) {
  29. cerr<<"Please give the file name."<<endl;
  30. return -1;
  31. }
  32. ifstream in(argv[1]);
  33. if(!in)
  34. {
  35. cerr<<"can't open input file"<<endl;
  36. return -1;
  37. }
  38. while (getline(in, line)) {
  39. PersonInfo info;
  40. record.clear();
  41. record.str(line);
  42. record >> info.name;
  43. while(record >> word)
  44. info.phones.push_back(word);
  45. people.push_back(info);
  46. }
  47. ostringstream os;
  48. for (const auto &entry : people) {
  49. ostringstream formatted, badNums;
  50. for(const auto &nums : entry.phones) {
  51. if (!valid(nums)) {
  52. badNums << " "<< nums;
  53. }
  54. else
  55. formatted << " " <<format(nums);
  56. }
  57. if (badNums.str().empty())
  58. os <<entry.name<<" "<<formatted.str()<<endl;
  59. else
  60. cerr<<"input error: "<<entry.name<<" invalid number(s) "<<badNums.str()<<endl;
  61. }
  62. cout<<os.str()<<endl;
  63. return 0;
  64. }

练习8.14:我们为什么将entry和nums定义为const auto&?

这两条语句分别适用范围for语句枚举people中所有项和每项的phones中的所有项。使用const表明在循环中不会改变这些项的值;auto是请求编译器依据vector元素类型来推断出entry和nums的类型,既简化代码又避免出错;使用引用的原因是,people和phones的元素分别是结构对象和字符串对象,使用引用即可避免对象拷贝。

 
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

2
0

练习8.1:编写函数。接受一个istream&参数,返回值类型也是istream&。此函数必须从给定流中读取数据,直至遇到文件结束标识时停止。

  1. #include <iostream>
  2. #include <stdexcept>
  3. using std::istream;
  4. using std::cin;
  5. using std::cout;
  6. using std::cerr;
  7. using std::endl;
  8. using std::runtime_error;
  9. istream &f(istream &in)
  10. {
  11. int v;
  12. while(in >> v, !in.eof())
  13. {
  14. if(in.bad())
  15. throw runtime_error("IO Stream error.");
  16. if(in.fail())
  17. {
  18. cerr<<"Data error! Please try again."<<endl;
  19. in.clear();
  20. in.ignore(100, '\n');
  21. continue;
  22. }
  23. cout<< v <<endl;
  24. }
  25. in.clear();
  26. return in;
  27. }
  28. int main()
  29. {
  30. cout<<"Please input some numbers, enter Ctrl+Z to end"<<endl;
  31. f(cin);
  32. return 0;
  33. }

练习8.3:什么情况下,下面的while循环会终止?

while (cin >> i) /*. . .*/

遇到文件结束符,或者遇到了IO流错误或者读入了无效数据。

练习8.4:编写函数,以读模式打开一个文件,将其内容读入到一个string的vector中,将每一行作为一个独立的元素存于vector中。

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. using std::cout;
  6. using std::endl;
  7. using std::string;
  8. using std::ifstream;
  9. using std::cerr;
  10. using std::vector;
  11. int main()
  12. {
  13. ifstream in("data.txt");
  14. if(!in) {
  15. cerr<<"Can't open the file."<<endl;
  16. return -1;
  17. }
  18. string line;
  19. vector<string> words;
  20. while(getline(in, line))
  21. words.push_back(line);
  22. in.close();
  23. vector<string>::const_iterator it = words.cbegin();
  24. while (it != words.cend())
  25. {
  26. cout<< *it <<endl;
  27. ++it;
  28. }
  29. return 0;
  30. }

练习8.5:重写上面的程序,将每个单词作为一个独立的元素进行存储。

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. using std::cout;
  6. using std::endl;
  7. using std::string;
  8. using std::ifstream;
  9. using std::cerr;
  10. using std::vector;
  11. int main()
  12. {
  13. ifstream in("data.txt");
  14. if(!in) {
  15. cerr<<"Can't open the file."<<endl;
  16. return -1;
  17. }
  18. string line;
  19. vector<string> words;
  20. while(in >> line)
  21. words.push_back(line);
  22. in.close();
  23. vector<string>::const_iterator it = words.cbegin();
  24. while (it != words.cend())
  25. {
  26. cout<< *it <<endl;
  27. ++it;
  28. }
  29. return 0;
  30. }

练习8.6:重写7.1.1节的书店程序,从一个文件中读取交易记录。将文件名作为一个参数传递给main。

  1. #include <iostream>
  2. #include <fstream>
  3. #include "Sales_data.h"
  4. using std::cout;
  5. using std::cerr;
  6. using std::ifstream;
  7. using std::endl;
  8. int main(int argc, char *argv[])
  9. {
  10. if (argc != 2) {
  11. cerr<< "Please give the file name."<<endl;
  12. return -1;
  13. }
  14. ifstream in(argv[1]);
  15. if (!in) {
  16. cerr<<"Can't open the file."<<endl;
  17. return -1;
  18. }
  19. Sales_data total;
  20. if (read(in, total)) {
  21. Sales_data trans;
  22. while(read(in ,trans)) {
  23. if(total.isbn() == trans.isbn())
  24. total.combine(trans);
  25. else {
  26. print(cout, total) << endl;
  27. total =trans;
  28. }
  29. }
  30. print(cout, total)<<endl;
  31. }
  32. else {
  33. cerr<<" No data?!"<<endl;
  34. }
  35. return 0;
  36. }

练习8.7:修改上一节的书店程序,将结果保存到一个文件中。将输出文件名作为第二个参数传递给main函数。

  1. #include <iostream>
  2. #include <fstream>
  3. #include "Sales_data.h"
  4. using std::cerr;
  5. using std::ifstream;
  6. using std::ofstream;
  7. using std::endl;
  8. int main(int argc, char *argv[])
  9. {
  10. if (argc != 3) {
  11. cerr<< "Please give the input file name and out file name."<<endl;
  12. return -1;
  13. }
  14. ifstream in(argv[1]);
  15. if (!in) {
  16. cerr<<"Can't open the file."<<endl;
  17. return -1;
  18. }
  19. ofstream out(argv[2]);
  20. if (!out) {
  21. cerr<<"can't open output file."<<endl;
  22. return -1;
  23. }
  24. Sales_data total;
  25. if (read(in, total)) {
  26. Sales_data trans;
  27. while(read(in ,trans)) {
  28. if(total.isbn() == trans.isbn())
  29. total.combine(trans);
  30. else {
  31. print(out, total) << endl;
  32. total =trans;
  33. }
  34. }
  35. print(out, total)<<endl;
  36. }
  37. else {
  38. cerr<<" No data?!"<<endl;
  39. }
  40. return 0;
  41. }

练习8.8:修改上一题的程序,将结果追加到给定的文件末尾。对同一个输出文件,运行程序至少两次,检验数据是否得以保留。

  1. #include <iostream>
  2. #include <fstream>
  3. #include "Sales_data.h"
  4. using std::cerr;
  5. using std::ifstream;
  6. using std::ofstream;
  7. using std::endl;
  8. int main(int argc, char *argv[])
  9. {
  10. if (argc != 3) {
  11. cerr<< "Please give the input file name and out file name."<<endl;
  12. return -1;
  13. }
  14. ifstream in(argv[1]);
  15. if (!in) {
  16. cerr<<"Can't open the file."<<endl;
  17. return -1;
  18. }
  19. ofstream out(argv[2], ofstream::app);
  20. if (!out) {
  21. cerr<<"can't open output file."<<endl;
  22. return -1;
  23. }
  24. Sales_data total;
  25. if (read(in, total)) {
  26. Sales_data trans;
  27. while(read(in ,trans)) {
  28. if(total.isbn() == trans.isbn())
  29. total.combine(trans);
  30. else {
  31. print(out, total) << endl;
  32. total =trans;
  33. }
  34. }
  35. print(out, total)<<endl;
  36. }
  37. else {
  38. cerr<<" No data?!"<<endl;
  39. }
  40. return 0;
  41. }

练习8.3.1:使用你为8.1.2节第一个练习所编写的函数打印一个istringstream对象的内容。

  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <stdexcept>
  5. using std::istream;
  6. using std::ostringstream;
  7. using std::istringstream;
  8. using std::string;
  9. using std::cout;
  10. using std::cerr;
  11. using std::endl;
  12. using std::runtime_error;
  13. istream &f(istream &in)
  14. {
  15. int v;
  16. while(in >> v, !in.eof())
  17. {
  18. if(in.bad())
  19. throw runtime_error("IO Stream error.");
  20. if(in.fail())
  21. {
  22. cerr<<"Data error! Please try again."<<endl;
  23. in.clear();
  24. in.ignore(100, '\n');
  25. continue;
  26. }
  27. cout<< v <<endl;
  28. }
  29. in.clear();
  30. return in;
  31. }
  32. int main()
  33. {
  34. ostringstream msg;
  35. msg<<"C++ Primer 5th edition"<<endl;
  36. istringstream in(msg.str());
  37. f(in);
  38. return 0;
  39. }

练习8.10:编写程序,将来自一个文件中的行保存在一个vector<string>中。然后使用一个istringstream从vector读取数据元素,每次读取一个单词。

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. using std::cerr;
  7. using std::endl;
  8. using std::cout;
  9. using std::ifstream;
  10. using std::istringstream;
  11. using std::string;
  12. using std::vector;
  13. int main()
  14. {
  15. ifstream in("Data.txt");
  16. if (!in) {
  17. cerr<<" Can't open input file."<<endl;
  18. return -1;
  19. }
  20. string line;
  21. vector<string> words;
  22. while (getline(in, line)) {
  23. words.push_back(line);
  24. }
  25. in.close();
  26. vector<string>::const_iterator it = words.begin();
  27. while( it != words.end()) {
  28. istringstream line_str(*it);
  29. string word;
  30. while(line_str >> word)
  31. cout<< endl;
  32. ++it;
  33. }
  34. return 0;
  35. }

练习8.11:本节的程序在外层while循环中定义了istringstream对象。如果record对象定义在循环之外,你需要对程序进行怎么样的修改?重写程序,将record的定义移到while循环之外,验证你设想的修改方法是否正确。

  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5. using std::cin;
  6. using std::istringstream;
  7. using std::string;
  8. using std::vector;
  9. struct PersonInfo {
  10. string name;
  11. vector<string> phones;
  12. };
  13. int main()
  14. {
  15. string line, word;
  16. vector<PersonInfo> people;
  17. istringstream record;
  18. while (getline(cin,line)) {
  19. PersonInfo info;
  20. record.clear();
  21. record.str(line);
  22. record >> info.name;
  23. while (record >> word)
  24. info.phones.push_back(word);
  25. people.push_back(info);
  26. }
  27. return 0;
  28. }

练习8.12:我们为什么没有在PersonInfo中使用类内初始化?

由于每个人的电话号数量不固定,因此更好的方式不是通过类内初始化指定人名和所有电话号码,而是在缺省初始化之后,在程序中设置人名并逐个添加电话号码。

练习8.13:重写本节的电话号码程序,从一个命名文件而非cin读取数据。

  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. using std::cerr;
  7. using std::endl;
  8. using std::cout;
  9. using std::ifstream;
  10. using std::istringstream;
  11. using std::ostringstream;
  12. using std::string;
  13. using std::vector;
  14. struct PersonInfo {
  15. string name;
  16. vector<string> phones;
  17. };
  18. string format(const string &s) { return s; }
  19. bool valid(const string &s)
  20. {
  21. return true;
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. string line, word;
  26. vector<PersonInfo> people;
  27. istringstream record;
  28. if (argc != 2) {
  29. cerr<<"Please give the file name."<<endl;
  30. return -1;
  31. }
  32. ifstream in(argv[1]);
  33. if(!in)
  34. {
  35. cerr<<"can't open input file"<<endl;
  36. return -1;
  37. }
  38. while (getline(in, line)) {
  39. PersonInfo info;
  40. record.clear();
  41. record.str(line);
  42. record >> info.name;
  43. while(record >> word)
  44. info.phones.push_back(word);
  45. people.push_back(info);
  46. }
  47. ostringstream os;
  48. for (const auto &entry : people) {
  49. ostringstream formatted, badNums;
  50. for(const auto &nums : entry.phones) {
  51. if (!valid(nums)) {
  52. badNums << " "<< nums;
  53. }
  54. else
  55. formatted << " " <<format(nums);
  56. }
  57. if (badNums.str().empty())
  58. os <<entry.name<<" "<<formatted.str()<<endl;
  59. else
  60. cerr<<"input error: "<<entry.name<<" invalid number(s) "<<badNums.str()<<endl;
  61. }
  62. cout<<os.str()<<endl;
  63. return 0;
  64. }

练习8.14:我们为什么将entry和nums定义为const auto&?

这两条语句分别适用范围for语句枚举people中所有项和每项的phones中的所有项。使用const表明在循环中不会改变这些项的值;auto是请求编译器依据vector元素类型来推断出entry和nums的类型,既简化代码又避免出错;使用引用的原因是,people和phones的元素分别是结构对象和字符串对象,使用引用即可避免对象拷贝。

 
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

2
0

C++中文件的读取操作,如何读取多行数据,如何一个一个的读取数据的更多相关文章

  1. kernel中文件的读写操作可以使用vfs_read()和vfs_write

    需要在Linux kernel--大多是在需要调试的驱动程序--中读写文件数据.在kernel中操作文件没有标准库可用,需要利用kernel的一些函数,这些函数主要有: filp_open() fil ...

  2. (六)kernel中文件的读写操作可以使用vfs_read()和vfs_write

    需要在Linux kernel--大多是在需要调试的驱动程序--中读写文件数据.在kernel中操作文件没有标准库可用,需要利用kernel的一些函数,这些函数主要有: filp_open() fil ...

  3. linux中文件I/O操作(系统I/O)

    我们都知道linux下所有设备都是以文件存在的,所以当我们需要用到这些设备的时候,首先就需要打开它们,下面我们来详细了解一下文件I/O操作. 用到的文件I/O有以下几个操作:打开文件.读文件.写文件. ...

  4. Python中文件的读写操作

    文件操作基本流程: 1. 介绍 计算机系统是由计算机硬件,操作系统,和应用程序三部分组成. 内存 存放不持久 硬盘 可以使数据持久化 文件操作  数据持久化的一种 全栈开发  框架类 2. 文件的操作 ...

  5. Python中文件路径名的操作

    1 文件路径名操作 对于文件路径名的操作在编程中是必不可少的,比如说,有时候要列举一个路径下的文件,那么首先就要获取一个路径,再就是路径名的一个拼接问题,通过字符串的拼接就可以得到一个路径名.Pyth ...

  6. 『无为则无心』Python基础 — 41、Python中文件的读写操作(一)

    目录 1.文件操作步骤 2.文件的读写操作 (1)文件的打开 (2)打开文件模式 (3)获取一个文件对象 (4)关于文件路径 1.文件操作步骤 当我们要读取或者写入文件时,我们需要打开文件,在操作完毕 ...

  7. 『无为则无心』Python基础 — 42、Python中文件的读写操作(二)

    目录 (5)文件对象方法(重点) 1)写方法 2)读方法 3)seek()方法 4)tell()方法 (6)关闭 (7)综合练习:读取大文件 (5)文件对象方法(重点) 1)写方法 @1.语法 对象对 ...

  8. python中文件的基础操作

    打开文件的三种方式: open(r'E:\学习日记\python\code\文件的简单操作.py') open('E:\\学习日记\\python\\code\\文件的简单操作.py') open(' ...

  9. python 中文件夹的操作

    文件有两个管家属性:路径和文件名. 路径指明了文件在磁盘的位置,文件名原点的后面部分称为扩展名(后缀),它指明了文件的类型. 一:文件夹操作 Python中os 模块可以处理文件夹 1,当前工作目录 ...

  10. Python中文件的读写操作的几种方法

    对文件的操作,步骤为:打开一个文件-->读取/写入内容-->保存文件 文件读写的3中模式 # 1.w 写模式,它是不能读的,如果用w模式打开一个已经存在的文件,会清空以前的文件内容,重新写 ...

随机推荐

  1. 3种常见的CSS页面布局--双飞翼布局、粘连布局、左右两列布局

    一.左右两列布局 1.代码如下,可先粘贴复制,自行运行 <!DOCTYPE html><html> <head> <meta charset="UT ...

  2. Springmvc-crud-02错误(添加出现中文乱码)

    错误: 在进行添加页面时使用post请求,输入中文时会出现乱码 原因: post请求不支持gbk格式,使用字符编码过滤器,设置为UTF-8编码即可 注意配置请求的字符集和响应字符集 解决方案:需要放在 ...

  3. Go语言基础之Path包与FilePath包

    文章引用自 path包的使用 package main; import ( "fmt" "path" ) //go语言path包的学习 func main() ...

  4. 第八届极客大挑战 Web-iPhone X

    题目: 解题思路: 第一次看到html里只有字其他啥也没有的题,一脸懵逼,学长提示抓包改包,于是开始我的苦逼解题. 0x01 抓包 0x02 改包 由于题目说只有iphoneX才能接受这个websit ...

  5. Java的单例模式(singleton)

    为什么需要单例?只因为国家的独生子女政策(当然现在可以生2个) 单例是一个很孤独的物种,因为它的类里面做多只有也仅只有它一个. 常见的是懒汉及饿汉模式, 1.懒汉,为什么这么叫,看看英文,原为lazy ...

  6. list的使用-Hdu 1276

    士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. SpringBoot 配置 Redis 多缓存名(不同缓存名缓存失效时间不同)

    import com.google.common.collect.ImmutableMap; import org.springframework.cache.CacheManager; import ...

  8. Linux系统常用运维命令汇总

    因为这段时间加入Java后台开发,所以对Java后台开发必会的Linux系统指令进行了熟悉这里进行汇总; tar 参数说明:-x: 解压 -z:有gzip属性 -v:显示所有过程 -f:文件名 -c: ...

  9. JS bind()方法、JS原生实现bind()

    一.arguments的含义 // arguments 是一个对应于传递给函数的参数的类数组对象 function a(){ console.log(arguments); } a(); // Arg ...

  10. 一个arctan积分的两种解法

    \[\Large\int_{0}^{1}\frac{\arctan x}{\sqrt{1-x^{2}}}\mathrm{d}x\] \(\Large\mathbf{Solution:}\) 首先第一种 ...