-2

I'm having some problems reading a string into an array. my file contains the following strings running horizontally down the page.

File:

dog
cat
rabbit
mouse

Code:

#include <string>
int i = 0;
using namespace std;
int main()
{
    FILE * input1;
    fopen_s(&input1, "C:\\Desktop\\test.dat", "r");
    string test_string;
    while (!feof(input1)) {
        fscanf_s(input1, "%s", test_string);
        i++;
    }
    return 0;
}

Any advice would be appreciated, Thanks!

Ahmad Khan
  • 2,655
  • 19
  • 25
  • Where is your array? – Sumeet Sep 20 '16 at 10:08
  • 1
    Why don't you use **C**, if you want to write such code? – Ahmad Khan Sep 20 '16 at 10:09
  • `test_string.data()` will allow you to use your string inside `fscanf_s`. But take care to reserve some memory with `test_string.resize(...)`. And take a look at `fstream` and `getline` which are "more" C++ – Garf365 Sep 20 '16 at 10:11
  • 3
    Counter question why don't you use c++, `std::ifstream` and `std::getline()`? – πάντα ῥεῖ Sep 20 '16 at 10:11
  • And if I understand well, you want `test_string` to be put inside an array. So take a look at `vector` or `list` or any other modern container – Garf365 Sep 20 '16 at 10:13
  • Thanks for the input everyone. @Sumeet I thought the array was was created using string test_string; (line 8). – Dr.WallMart Sep 20 '16 at 10:14
  • Thanks @Garf365 , I was trying to make test_string my array to store my values in. – Dr.WallMart Sep 20 '16 at 10:16
  • 1
    Doesn't the fopen_s need a pointer to FILE ... _FILE *_ ? – blackpen Sep 20 '16 at 10:17
  • No, line 8 doesn't create an array. It just create a string. replace by `std::vector test_strings` and use it like `test_string[i]` to get _i_ th string – Garf365 Sep 20 '16 at 10:17
  • I see, would line 10 then become `fscanf_s(input1, "%s", test_string[i]);` ?@Garf365 – Dr.WallMart Sep 20 '16 at 10:22
  • Not totally, maybe I make a misteak, but for me, `fscanf_s` doesn't accept a string as parameter. You have to use `test_string[i].data()` or `&test_string[i][0]` *and* before resize to allocate memory (`test_string[i].resize(50)` to reserve 50 characters). After `fscanf_s` you have to resize with correct size (`test_string[i].resize(strlen(test_string[i].c_str()));`. But to avoid all this laborious steps, consider to use `ifstream` and `getline` instead of `fopen` and `fscanf_s` – Garf365 Sep 20 '16 at 10:27
  • Is there really a need to use `fscanf`? Can't you simply use `ifstream` and `getline`? – Ahmad Khan Sep 20 '16 at 10:33
  • The only reason I used fscanf is because i'm familiar with the expression. – Dr.WallMart Sep 20 '16 at 10:36
  • Without going into "why write such code?" ... 1) include the stdio.h 2) use _input1=fopen("test.txt", "r");_ 3) use _char test_string[1024]_ instead of _string_ 4) use _fscanf(input1, "%s", test_string);_ 5) In the end print the counter _i_ (I assume you were trying to count lines?) 6) Use compiler warnings extensively (something like _-Wall_) – blackpen Sep 20 '16 at 10:41
  • 1
    Then you should get familiar with [`fstream`](http://www.cplusplus.com/reference/fstream/) – Ahmad Khan Sep 20 '16 at 10:44
  • 1
    See: [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/q/5431941/253056) – Paul R Sep 20 '16 at 11:11

1 Answers1

0

You should use ifstream and std::getline

Now, I'm going to walk you through reading lines from the file using ifstream

Include fstream to use ifstream.

#include <fstream>

Opening a file:

To open a file, create an object of ifstream, and call it's method open and pass the filename as parameter. ifstream opens a file to read from it. (To write in a file, you can use ofstream)

ifstream fin;
fin.open("C:\\Desktop\\test.dat");

Or you can simply pass the filename to the constructor to create an object of ifstream and open a file.

ifstream fin("C:\\Desktop\\test.dat");

Reading from the file:

You can use stream extraction operator (>>) to read from the file, just like you use cin

int a;
fin >> a;

To read a line from a file using the above created fin (using a char array)

char arr[100];
fin.getline(arr, 100);

Better yet, you should use std::string instead or char arrays, using std::string, you can read a line using std::getline

string testString;
getline(fin, testString);

Now, let's change your code to use ifstream and getline

#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    int i = 0;
    ifstream input1;
    input1.open("C:\\Desktop\\test.dat");
    string test_string;

    while (getline(input1, test_string)) {
        i++;
    }

    return 0;
}
Ahmad Khan
  • 2,655
  • 19
  • 25