C ++ reads and writes txt text.

Source: Internet
Author: User
Tags visual studio 2010

C ++ reads and writes txt text.

Input and output are basic skills for every programmer, especially for text input and output. Recently, I have made some summary in this regard, but it is not very comprehensive. I hope to gradually supplement it in the future study and work process and accumulate little bit by bit. P.S. The weather is good today, the smog is scattered, the sky is clear, the weather is good, good mood.

I. Write operations

// Set. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include" iostream "# include" string "# include" fstream "using namespace std; int _ tmain (int argc, _ TCHAR * argv []) {const string data = (string) "hello" + "world"; ofstream out (data, ofstream: app); out <3 <endl; out. close (); return 0 ;}



Ii. Read operations

// Set. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include" iostream "# include" string "# include" fstream "# include" vector "# include" sstream "using namespace std; struct personalinfo {string name; vector <string> phones ;}; int _ tmain (int argc, _ TCHAR * argv []) {const string data = "C: \ Users \ helei \ Documents \ Visual Studio 2010 \ Projects \ set \ Release \ hello + world "; ifstream out (data, ofstream :: app); string line, word; vector <personalinfo> people; while (getline (out, line) {personalinfo info; istringstream record (line); record> info. name; while (record> word) info. phones. push_back (word); people. push_back (info);} cout <people [0]. name <"" <people [0]. phones [0] <"<people [0]. phones [1] <endl; cout <people [1]. name <"" <people [1]. phones [0] <endl; cout <people [2]. name <"" <people [2]. phones [0] <"<people [2]. phones [1] <"<people [2]. phones [2] <endl; out. close (); return 0 ;}



C Language Reading of TXT text files,

Your fscanf (fp, "% c", s)

Remove the first character

X1234
X4567
Press any key to continue

# Include "stdio. h"
# Include "string. h"
Main ()
{
FILE * fp;
Char line [100] = {0}, * point;
Char s [50] = {0 };
Int I, length;
Fp = fopen ("2.txt"," rb ");
While (fgets (line, 100, fp )! = NULL)
{
Length = strlen (line );
Point = line;
For (I = 0; I <length; I ++)
{
Printf ("% c", line [I]);
}
Point ++;
}
}

C language for reading and writing txt documents

For file reading and writing, you must master the file operation functions, namely open, close, fscanf, fprintf. Compile a simple function for you! For example, in path D: \ a.txt, the number of a.txt files is 123. How can I print this number out and print it out! The Code is as follows: # include <stdio. h> int main () {int x; FILE * file = fopen ("D: \ a.txt", "r"); // r is the abbreviation of read in English, read fscanf (file, "% d", & x); printf ("% d", x); return 0 ;}enter a number from the screen and write it into D: /a.txt # include <stdio. h> int main () {int x; FILE * file = open ("D: \ a.txt", "w"); scanf ("% d", & x ); fprintf (file, "% d", x); return 0; open a.txt to check if the success is successful!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.