Question

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;
int main(int argc, char* argv[])
{
ifstream ifile;
char fName[30][30];
long int uTime[30][10];
ifile.open("sync.log");
char ch;
string str = "";
if(ifile.fail())
{
    cout<<"Invalid File Name!";
    system("pause");
    exit(1);

}
int i = 0;
while(!ifile.eof())
{
    getline(ifile, str);
    cout<<str<<endl;
    sscanf(str.c_str(),"%ld %[^\n]",&uTime[i],fName[i]);

    i++;
    str = "";

}

ifile.close();
system("pause");    
cout<<"Output:"<<endl;
for(i = 0 ;  i < 2 ; i ++)
{
    cout<<uTime[i]<<"   ";
    cout<<fName[i];
    cout<<endl;
}


getch();
return 0;

}

File : sync.log
Format:
1399865017 Test1.txt
1399865017 Test1.txt
so here is my full code and i have sync.log file in root directory where the VC++ saved the projects...
It must be stored like this in array after Reading from File
uTime[0] = 1399865017;
fName[0] = "Test1.txt";
fName[1] = "Test1.txt";

with this above code i am getting
uTime[0] = 0012F6B0 and fName[0] = "Test1.txt"

and i want this
uTime[0] = 1399865017;
fName[0] = "Test1.txt";

Was it helpful?

Solution

I think you meant to use:

long int uTime[30];

instead of

long int uTime[30][10];

With that, the line that reads data into uTime and the line that write uTime to cout would make sense.

OTHER TIPS

Given that you have:

long int uTime[30][10];

when you do:

cout << uTime[i] << …

you are printing a pointer to an array of 10 long. That pointer is formatted in hex; that is why you are getting hex output.

You need to fix the sscanf() call — you've got a bogus argument of &uTime[i] there — and the output too. Since it is not clear why uTime is a 2D array, it isn't easy to tell you how to fix it, but the simplest solution would be to drop the [10] from the array definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top