Link to home
Start Free TrialLog in
Avatar of vjabhi
vjabhi

asked on

read text file using c/c++

Hi All,
I have to read text file which has data in following manner,
2345  zxc  123344 abchfdfdfdg
I need to read fixed length and store same into variable,
Say first 6 character, then next 5 and so on&
This will also include spaces.

Whats the best way to do this??
Is fgetc is right way to do this?

Please suggest.

Thanks in advance.

Thanks,
Abhi



Avatar of Infinity08
Infinity08
Flag of Belgium image

Do a getline, and then process the line you got. Since you want to split it up at fixed indexes, that shouldn't be too hard ... substr should help :

        http://www.cplusplus.com/reference/string/getline.html
        http://www.cplusplus.com/reference/string/string/substr.html

Don't forget to check out the example code at the bottom of those pages.

For reading a file, check out this tutorial :

        http://www.cplusplus.com/doc/tutorial/files.html
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of henderbops
henderbops

You could read 1 character at a time and loop through until you find what you want..

#include <cstring>
#include <fstream.h>
using namespace std;

string readCharacters(int charStart, int charEnd, char* filename)
{
   string ret;
   ifstream file (filename); // open file stream for reading using filename specified
   if ( !file.is_open() || !file.good() )
   {
      // Error has occured opening file: Handle it.
   }
   else
   }
      char c;
      for (int a = 0; a < charEnd; a++)
      {
         c = (char)file.get(); // Get the character at this point in file
         if ( a > charStart && a < charEnd ) // if the character
         {
            ret += c; // append character to string
         }
      }
   }
   file.close();
   return ret;
}

That is roughly how you could do it in C++, not sure if it works as I haven't tested it.
You would use this function to get characters from charStart to charEnd in the file specified by filename.. i.e.

string a = readCharacters(0,4,"C:\\file.txt");
string b = readCharacters(5,8,"C:\\file.txt");
etc
a simple program to read file line by line
        #include <stdio.h>
       #include <stdlib.h>

       int main(void)
       {
            FILE * fp;
            char * line = NULL;
            size_t len = 0;
            ssize_t read;
            fp = fopen("file.txt", "r");
            if (fp == NULL)
                 exit(EXIT_FAILURE);
            while ((read = getline(&line, &len, fp)) != -1) {
                 printf("Retrieved line of length %zu :\n", read);
                 printf("%s", line);
            }
            if (line)
                 free(line);
            return EXIT_SUCCESS;
       }