Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Few Questions Regarding Fscanf

align scanf

  • Please log in to reply
14 replies to this topic

#1 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 01 July 2012 - 08:07 AM

Hi guys, i have a set of questions for you...

I need to read some files, ofc, using language C.

Let's say i have a file withe N lines, with every line containing data in the following patern (some random strings and numbers)
6.5.2004		   312***			 01:06:12	01:07:22	70	 1
7.5.2004		   312***			 22:16:28	22:24:22	474	2
17.5.2004		 0113027***		 23:10:53	23:14:57	244	16
22.5.2004 99359888556*** 19:32:23 19:33:47 84 65

so, i read the file and i need to make an output file, with aligned data, in form of
312***		70
312***		474
etc

so, my question is how do i align numbers in the right column?
how do in general read a lot of spaces or TAB signs, using fscanf?

EDIT:
forum seems to swallows the space signs between numbers... anyway between every "block" of words is a number of space signs >1
  • 0

#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 01 July 2012 - 02:58 PM

In scanf you can be very specific in what you want to be included or excluded from the capture,
let me give you and example of that :

This scanf will capture everything not being a tab or an new line into your yourString variable :
scanf("%[^[b]\[/b]t\n]",yourString);

and this one will capture only the specific characters provided
scanf("%[ a-zA-Z0-9+*/-]",string);

as you can see, including your desired characters, either to be included or excluded inside brackets you can make very specific text captures, when you start your capture string with ^ it means that what follows is the non-wanted char list, if no ^ you are providing the good to go char list, also a-z means from a to z (lower case), and you can do that range thing with numbers too as you can see with the 0-9

so, did that open a way for a solution ? :D

oh, you must quit the in the first example, i put that there cause the forum eats the \{fake space}t symbol, which is the TAB character
  • 0

#3 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 01 July 2012 - 06:22 PM

Agree with Blackrabbit.

Note that with scanf/fscanf whitespace (blank, newline or tab) characters are automatically ignored. Now you only need to ignored the * characters. So you can parse a line using 6 fscanf (with the [^*] modifier in the second one).

Then if you want to convert string to number, you can use ato[i/f/l] functions.

To print aligned value, you can use printf/fprintf function with specifying 'width' modifier. As example, prinf("%5d %-d", 100, 100) will print the fist 100 with 5 width characters right aligned whereas second 100 will be left align.
  • 0

#4 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 July 2012 - 04:34 AM

Agree with Blackrabbit.

Note that with scanf/fscanf whitespace (blank, newline or tab) characters are automatically ignored. Now you only need to ignored the * characters. So you can parse a line using 6 fscanf (with the [^*] modifier in the second one).

Then if you want to convert string to number, you can use ato[i/f/l] functions.

To print aligned value, you can use printf/fprintf function with specifying 'width' modifier. As example, prinf("%5d %-d", 100, 100) will print the fist 100 with 5 width characters right aligned whereas second 100 will be left align.


I didn't know that fscanf ignores the spaces.
So if i have
John Smith
and say fscanf (file, "%s %s",&string1,&string2);
string1 will be John, and string2 will be Smith.
  • 0

#5 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 02 July 2012 - 04:35 AM

yeah, that's what I know.
  • 0

#6 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 July 2012 - 05:10 AM

Kernelcoder,can i ask you one question more, which isn't related to double linked lists :)

When i have a double linked list in C, and i have let's say 6 elements:
i need to delete all elements that have the same content, except the last one.

For example, if i have elements a,b,c,d,e,f, and elements a,b,and e are the same (contain the same data) - the new list would contain only c,d,e0 and f.

A little help please?
  • 0

#7 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 02 July 2012 - 05:41 AM

Please read my comment in this this post that said about linked list with example. So as of the example from you, those 6 nodes are connect as follows..
a--><--b--><--c--><--d--><--e--><--f
So to delete a node, more precisely to delete the node c what you need to do is: you need to delete the link in between b & c and c& d and then you need to make link between b & d. To delete root or last node, you need to take extra considerations.

Now come to your problem. To delete all the nodes that have the same content/data except the last one, you need to follow the logic below.
  • First keep a variable of type your linked-list node's pointer. Let us call it 'preFoundNode' and set null/zero to it.
  • Now start begin search over the linked list.
  • If you found a match to your data you are searching for: first delete the node in variable 'preFoundNode' if it is not null/zero. Then **ign the currently found node to it.
And that't it. Following is the pseudo-code for it.
function deleteAllButLast(root, data)
begin
   preFoundNode := null;
   deletedCount:= 0;
   while (root is not null)
   begin
      IF (root's content is same as data)
      begin
         IF (preFoundNode is not null)
         begin
            delete preFound;// I think you need to write a function to delete a node from the linked list and call it.
            deletedCount:= deletedCount + 1
         end
         preFoundNode: = root;
      end
      root: = root->next //(root's next)
   end
end
I hope it will help you.
  • 0

#8 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 July 2012 - 07:23 AM

kernelcoder thanks a bunch!

i have one more question and i'm done for today :)

I have a file that contains a list of products.
Every line contains product id and it's amount (to be shipped)...

So i have a list like
100 10
100 21
110 15
120 20
120 1
130 5

where 100,110,120 and 130 are product ids, and the numbers in the right column are amounts.
I need to read the file (and without the use of linked lists) to create an output file, where i would put all the amounts of one product id to a same line, that is
the output file looks like
100 31
110 15
120 21
130 5

and i know that the same products ids are in consecutive lines.
that is, i can't have
100 10
110 15
100 20
120 25
100 25

And i can read the input file only once.

So, i thought about reading line by line, and comparing each line to the following one.
I don't know if the logic is good, but i can't seem to write a proper code for this logic.
So, if anyone could help, i would be grateful.
  • 0

#9 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 02 July 2012 - 08:11 AM

Is there any information about how many entries will be there in input file? Is there any constraints about using array or other constraints?

So, I think you can use a 2D array (like int products[][2]) with only 2 columns where the first column (product[0][i]) will contain the product id and the second column will contain the sum of the all the counts in the input file for that id. The logic will be when you will read in an entry [product id and its count perhaps using fscanf("%d %d", &product_id, &count)] from the file, you will search in the array whether the id exist: if yes, add on the the amount for that id; if not exist, create a new entry for that product in the array with setting the count.
  • 0

#10 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 July 2012 - 08:12 AM

The number of lines/entries in the file is not known.
Arrays shoudn't be used.

Only sure thing is that data about the same products is in consecutive lines in the file.
As i said, in the example - i can have either one line, either n lines about the same product
and they will all be one after another.

Like
100 1
100 5
100 6
100 8
  • 0

#11 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 02 July 2012 - 08:28 AM

Hmm. Then I think you don't need to use an array to solve this problem. The logic is simple: You need to keep only two variables -- int product_id and int product_amount and the initial value for them are -1 and 0. Now read each line, compare the current product id (from the file) with the product_id; If they are same, add the amount (from file) to product_amount. If they are not same and if product_id is greater than -1, write the product_id and product_amount to output file in each line.

Here is the pseudo-code for it. **ume that both input and output file are opened properly.

funtion main()
begin
initialize two variables -- product_id:= -1 and product_amount = 0;
while(input file is not reached to end)
        begin
        read two values from input file and store it in id and amount variable
        If (id is not same as product_id)
                begin
                If (product_id is not -1)
                        begin
                        print both product_id and product value in output file
                        end

                product_id := id
                product_amount := amount
                end
        else
                begin
                product_amount := product_amount + amount
                end
        end

If (product_id is not -1) 
        begin
        print both product_id and product value in output file
        end
end

  • 0

#12 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 July 2012 - 10:03 AM

Yes, but i have several product id's and their amounts, ie, i have input file

100 10
100 20
100 30
101 10
101 8
103 5
104 2
104 4

and the output file should look like
100 60
101 18
103 5
104 6
  • 0





Also tagged with one or more of these keywords: align, scanf

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download