View line by line in C and C ++?

I want to read line by line from a file in C or C ++, and I know how to do this when I assume a fixed line size, but is there an easy way to somehow calculate or get the right size for a line or all lines in a file ? (Reading word by word, while the new line is also not suitable for me, if someone can do it this way.)

+5
source share
7 answers

If you use a streaming reader, all of this will be hidden from you. See getline. The example below is based on the code here .

// getline with strings
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string str;
  ifstream ifs("data.txt");
  getline (ifs,str);
  cout << "first line of the file is " << str << ".\n";
}
+7
source

C, POSIX 2008 (, Linux), POSIX getline() . , , , , , .

++ std::getline().

, , ( C ++ ), , , .

, - , , , .

+4

IFStream getline .

http://www.cplusplus.com/doc/tutorial/files/

int main () {
    string line;
    ifstream myfile ("example.txt");
    if (myfile.is_open())
    {
        while ( myfile.good() )
        {
            getline (myfile,line);
            cout << line << endl;
        }
        myfile.close();
    }
    else cout << "Unable to open file"; 

    return 0;
}
+3

, . , , .

c fgets . n , . n, .

. .

, :

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

int main()
{
   FILE * pFile;

   const int n = 5;
   char mystring [n];
   int lineLength = 0;

   pFile = fopen ("myfile.txt" , "r");
   if (pFile == NULL) 
   {
       perror ("Error opening file");
   }
   else 
   {

        do 
        {
            fgets (mystring , n , pFile);
            puts (mystring);    
                    lineLength += strlen(mystring); 
        } while(mystring[strlen ( mystring)-1] != '\n' && !feof(pFile));

       fclose (pFile);
   }

   printf("Line Length: %d\n", lineLength);
   return 0;
}
+1

++ std::getline, '\n'. C fgets , '\n', , .

++:

std::ifstream file("myfile.txt");
std::string line;
std::getline(file, line);
std::cout << line;

// I didn't test this code I just made it off the top of my head.
FILE* file = fopen("myfile.txt", "r");
size_t cap = 256;
size_t len = 0;
char* line = malloc(cap);

for (;;) {
    fgets(&line[len], cap - len, file);
    len = strlen(line);
    if (line[len-1] != '\n' && !feof(file)) {
        cap <<= 1;
        line = realloc(line, cap);
    } else {
        break;
    }
}

printf("%s", line);
0

getline - POSIX, , ANSI ():

const char* getline(FILE *f,char **r)
{
  char t[100];
  if( feof(f) )
    return 0;
  **r=0;
  while( fgets(t,100,f) )
  {
    char *p=strchr(t,'\n');
    if( p )
    {
      *p=0;
      if( (p=strchr(t,'\r')) ) *p=0;
      *r=realloc(*r,strlen(*r)+1+strlen(t));
      strcat(*r,t);
      return *r;
    }
    else
    {
      if( (p=strchr(t,'\r')) ) *p=0;
      *r=realloc(*r,strlen(*r)+1+strlen(t));
      strcat(*r,t);
    }
  }
  return feof(f)?(**r?*r:0):*r;
}

:

  char *line,*buffer = malloc(100);
  FILE *f=fopen("yourfile.txt","rb");
  if( !f ) return;
  setvbuf(f,0,_IOLBF,4096);
  while( (line=getline(f,&buffer)) )
    puts(line);
  fclose(f);
  free(buffer);

Windows Unix-textfiles, Unix Unix Windows-textfiles

0

++ , std- :

#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>

struct getline :
  public std::iterator<std::input_iterator_tag, std::string>
{
    std::istream* in;
    std::string line;
    getline(std::istream& in) : in(&in) {
        ++*this;
    }
    getline() : in(0) {
    }
    getline& operator++() {
        if(in && !std::getline(*in, line)) in = 0;
    }
    std::string operator*() const {
        return line;
    }
    bool operator!=(const getline& rhs) const {
        return !in != !rhs.in;
    }
};

int main() {
    std::vector<std::string> v;
    std::copy(getline(std::cin), getline(), std::back_inserter(v));
}
0
source

All Articles