C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 10th, 2012, 01:18 AM
JamesA1 JamesA1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 12 JamesA1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 41 sec
Reputation Power: 0
Reading data from a 4 column file into an interger pointer variable

Hi,

I know how to read data from a text file and to store it in a variable.

I now have to read data from a 4 column file where each column is separated by a whitespace and store the data from the second column into an interger pointer variable. The length of the column is known in advance and is always fixed.

Can somone point me in the right direction by providing a snippet of the line that reads data from the 2nd column and stores into the interger pointer variable? Thats where I am stuck. I am not a programmer but rather someone who has to write code occasionally so this particular line is baffling me!

Reply With Quote
  #2  
Old September 10th, 2012, 02:02 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 34 m 16 sec
Reputation Power: 1974
What language? This forum deals with C (in its various forms; eg C89, C99, Objective-C), C++, and C#. We would commonly assume C as per the 1989 ISO standard (AKA "C89") and what we would answer would also apply to C++, but not to C# and possibly not to Objective-C.

Let's assume C. Using fscanf would do the job, since each column is separated by whitespace. However, I would recommend first reading in the entire line and then processing that line. Look into fgets() to read an entire line of text from a file. Then look into sscanf to process the second column of that line of text.

BTW, *scanf and *printf are families of functions which all operate very similarly, but one variant works with the standard terminal files (stdin and stdout), another with files (substituting "f" for that "*"), another with strings (substituting "s" for that "*").

Find out about those functions and how to use them either with your compiler's help files or by Google'ing on man page function-name.

Once you know what function(s) to use, you can then Google for sample code (good luck on that! Like finding an old high school friend using a phone book.). Or play with the functions to figure them out.


Of course, if you're trying to do this with C# or something else that does not have *scanf (C++ instructional books won't ever tell you this, but C++ can work perfectly well with *scanf), then what I just told you will mean nothing. But then, shouldn't you have told us up-front what you're using?

Last edited by dwise1_aol : September 10th, 2012 at 02:04 AM.

Reply With Quote
  #3  
Old September 10th, 2012, 02:49 AM
JamesA1 JamesA1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 12 JamesA1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 41 sec
Reputation Power: 0
Quote:
Originally Posted by dwise1_aol
What language? This forum deals with C (in its various forms; eg C89, C99, Objective-C), C++, and C#. We would commonly assume C as per the 1989 ISO standard (AKA "C89") and what we would answer would also apply to C++, but not to C# and possibly not to Objective-C.


I meant the C language, without stating what I think was quite the obvious given that this is a C Forum.

Reply With Quote
  #4  
Old September 10th, 2012, 03:32 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 34 m 16 sec
Reputation Power: 1974
Look towards the top of the page for this text:
Quote:
C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.

Or, to take our inspiration from Chief Inspector Jacques Clouseau (from early in the movie, A Shot in the Dark:
Quote:
I assume nothing. I suspect everything.

IOW, given what this very forum says about itself, it is not "quite the obvious given that this is a C Forum".

However, given your response, I feel free to assume that you are indeed talking about C itself. That being the case, I hope that my reply was helpful.

Reply With Quote
  #5  
Old September 10th, 2012, 03:43 AM
JamesA1 JamesA1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 12 JamesA1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 41 sec
Reputation Power: 0
Yes, thanks. I have started reading on fscanf and fgets and am currently experimenting with them.

Reply With Quote
  #6  
Old September 10th, 2012, 03:52 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 34 m 16 sec
Reputation Power: 1974
Just a quibble: it's either fscanf, or a combination of fgets and sscanf.

Since fscanf would be sensitive to how many columns there are, I would personally gravitate towards fgets and sscanf. Please think it through and see what I mean.

Reply With Quote
  #7  
Old September 10th, 2012, 06:02 AM
JamesA1 JamesA1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 12 JamesA1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 41 sec
Reputation Power: 0
Hi,

Here is what I have written so far. I get some results but not exactrly what I want.
If I put the result of fscanf as 1, then I can only the 1st line (with all four columns ) printed. Other entries are 0.

If I put result of fscanf as 4 (which is what it should be since I am reading 4 values), the the LAST line of data file is printed (with 4 columns) and all other entries are 0. Do you think you can spot where I am wrong?

Mv data file has intergers in the first two columns and doubles in the last two. There are 640 entries in the data file.

int main(int argc, char *argv[]){

FILE *in;
int column[640];
int column2[640];
double column3[640];
double column4[640];

// Specify file to read
sprintf(filename, "log-1.dat");
in = fopen(filename, "r");

if ( in ){
while (fscanf(in, "%d %d %lf %lf", scan, scan2, scan3, scan4) == 4){}
}

for ( int i = 0; i < 10; i++ ){
printf("array[%d] = %d, %d, %lf, %lf\n", i, scan[i], scan2[i], scan3[i], scan4[i]);
}

return 0;
}

Reply With Quote
  #8  
Old September 10th, 2012, 09:29 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 34 m 16 sec
Reputation Power: 1974
*scanf needs addresses where it can store the converted values. The address operator is &.

What are the declarations for scan, scan2, scan3, scan4, and scan[]?

Also, please use code tags when you post code.

Reply With Quote
  #9  
Old September 10th, 2012, 09:44 AM
JamesA1 JamesA1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 12 JamesA1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 41 sec
Reputation Power: 0
Hi,

Sorry about not using the code tag. Also, scan[i] corressponds to the columns of data in the text file. Four columns so four scan i's. Thing is that after compiling my code, I copied and pasted and thought of renaming all the scan i's to columns to make it better understood but in the process I missed out renaming all

The code works but only for the first or last line of the data file. I have tried searching high and low and have come across lots of similar questions but without any solutions

Code:
int main(int argc, char *argv[]){

FILE *in;
int scan[640];
int scan2[640];
double scan3[640];
double scan4[640];

// Specify file to read
sprintf(filename, "log-1.dat");
in = fopen(filename, "r");

if ( in ){
   while (fscanf(in, "%d %d %lf %lf", scan, scan2, scan3, scan4) == 4){}
}

// printing out first 10 values only
for ( int i = 0; i < 10; i++ ){
printf("array[%d] = %d, %d, %lf, %lf\n", i, scan[i], scan2[i], scan3[i], scan4[i]);
}

return 0;
}

Reply With Quote
  #10  
Old September 10th, 2012, 10:31 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,142 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 4 Days 34 m 16 sec
Reputation Power: 1974
Given:
Code:
int scan[640];
int scan2[640];
double scan3[640];
double scan4[640];

this line of code,
while (fscanf(in, "%d %d %lf %lf", scan, scan2, scan3, scan4) == 4){}
makes absolutely no sense whatsoever.

You might possibly have wanted to use some iteration subscript such as n, in which case it should have been:
while (fscanf(in, "%d %d %lf %lf", &scan[n], &scan2[n], &scan3[n], &scan4[n]) == 4){}
Please note the use of the address operator, &, to get the address of that particular element in the array, as well as the subscripting of the array to access each individual element. Very, very basic programming concepts (well, the array subscripting at least, though the address operator isn't exactly rocket science).

What you had done was to give fscanf array names, which are treated the same as addresses. Only the address an array name provides is only to the first element in the array.

Reply With Quote
  #11  
Old September 11th, 2012, 08:16 AM
JamesA1 JamesA1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 12 JamesA1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 18 m 41 sec
Reputation Power: 0
Thanks for your help so far but I gave up and demanded the format of the data file to be changed to a 1 column file. Now it makes my life easier.

Thanks again for the pointers you gave,

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Reading data from a 4 column file into an interger pointer variable

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap