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 December 22nd, 2012, 02:52 AM
fred_benrold fred_benrold is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 fred_benrold User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 32 sec
Reputation Power: 0
Problem with reading a file as a string

hi , this is a question in c programming.
#include <stdio.h>
int main (void)
{
FILE *Math_Expression;
int counter = 0;
int total = 0;
float average;
char n;
Math_Expression = fopen("pf1.txt", "r");
if (Math_Expression == NULL)
{
printf("Can't open numbers.txt\n");
exit(1);
}
while (fscanf(Math_Expression, "%s", &n) == 1)
{
printf("%s",n);
counter++;
}


fclose(Math_Expression);

printf("There were %d characters in the file\n", counter);

;

system("pause");
return 0;
}


The code above , doesnt compile.
If i change the "%s " to "*c" it works; why is that ?
Also the real thing i need to do is to be able to read in values from the file as string , and then play around with the values depending on their type ( operator like *,/,+,- etc or if its a decimal ) . The problem i face is if i need to read in the values as "%s", i will be stuck fiddling with a vraiable with an "&" sign .
How do i read in the values of the files regardles of it being an operator or a number , and then check the string for its type ( *,+,-,3.2).
I ran into trouble trying to store the values of the array as a string, i got a an error when i used the strcpy function to store the values from the file into a string , strcpy ( char, &n) ..where &n is the current value read from the file . The error was " [Warning] passing arg 1 of `strcpy' makes pointer from integer without a cast ".
Also how do i get rid of the & ( pointer adress) ?
As i have to extract values from the file as a string and then operate on them , as in compare them or add them or subtract them etc .; how do i do do these without getting the same "makes pointer without a cast error ".

Please help me as soon as possible.

Any feedback will be appreciated xD .

Reply With Quote
  #2  
Old December 22nd, 2012, 08:17 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,808 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 17 h 42 m 14 sec
Reputation Power: 1800
n is a single character not an array.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Problem with reading a file as a string

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