473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

using sscanf

Hi

I need to read a few lines out of a text file. The first line is 'n word ("add" or "multiply") the rest of the lines is numbers. If the first line is add then i need to add all the numbers and if the first line is multiply the i need to multiply all the numbers. The assignment says to use sscanf to convert from string to int. Can you give me some guidelines on how to use sscanf. At the moment my code is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. string *line;
  3.     int sum, tot = 0;
  4.  
  5.     getline(file1, line);
  6.     if(line == "ADD")
  7.     {
  8.         getline(file1, line);
  9.         while(line != "/n")
  10.         {
  11.     sum = sscanf(line);
  12.     tot = tot + sum;
  13.         }
  14.     }
  15.     else if(line == "MULTIPLY")
  16.     {
  17.         getline(file1, line);
  18.         while(line != "/n")
  19.         {
  20.     sum = sscanf(line);
  21.     tot = tot * sum;
  22.         }
  23.      }
  24.  
Feb 16 '07 #1
1 2590
sicarie
4,677 Expert Mod 4TB
Hi

I need to read a few lines out of a text file. The first line is 'n word ("add" or "multiply") the rest of the lines is numbers. If the first line is add then i need to add all the numbers and if the first line is multiply the i need to multiply all the numbers. The assignment says to use sscanf to convert from string to int. Can you give me some guidelines on how to use sscanf. At the moment my code is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. string *line;
  3.     int sum, tot = 0;
  4.  
  5.     getline(file1, line);
  6.     if(line == "ADD")
  7.     {
  8.         getline(file1, line);
  9.         while(line != "/n")
  10.         {
  11.     sum = sscanf(line);
  12.     tot = tot + sum;
  13.         }
  14.     }
  15.     else if(line == "MULTIPLY")
  16.     {
  17.         getline(file1, line);
  18.         while(line != "/n")
  19.         {
  20.     sum = sscanf(line);
  21.     tot = tot * sum;
  22.         }
  23.      }
  24.  
This should give you a good overview of sscanf() and some examples on how to use it.
Feb 16 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: baumann | last post by:
hi, 1) first test program code #include <stdio.h> int main(void) { char * file = "aaa 23 32 m 2.23 ammasd"; int i2,i3;
4
by: baumann | last post by:
hi all there has 2 program 1) the first test program code #include <stdio.h> int main(void) {
22
by: Superfox il Volpone | last post by:
Hello I have some problem with sscanf, I tryed this code but it doesn't works : char* stringa = "18/2005" char mese; char anno; int i_letture; i_letture = sscanf(stringa, "%2s/%4s",...
1
by: ziroi | last post by:
Hello! I am new here to the forums and still learning C. I understand your policies on coursework, but I have an error that I've run around and around and can't figure out where my problem is...
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
7
by: gio | last post by:
suppose I have: .... char str1; char str2; int ret; fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0' ret=sscanf(str1, "%s", str2); ....
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
69
by: Horacius ReX | last post by:
Hi, I have the following program structure: main ..... int A=5; int* ptr= A; (so at this point ptr stores address of A) ..... .....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.