473,395 Members | 1,689 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,395 software developers and data experts.

How to copy stream of data to a 2D array?

Hi All

I want to copy a stream of data to 2D array. I wrote the following
program which was not working as i expected.

Code


#include <stdio.h>


char a[3][6];
char b[50]={"abcdefghijklmnopqrst"};
main()
{
int i=0;


for(i=0;i<3;i++)
sscanf(b, "%6c", a[i]);


for(i=0;i<3;i++)
printf("%s\n", a[i]);



}


Output

abcdefabcdefabcdef
abcdefabcdef
abcdef


But I want to have output like this


abcdef
abcdef
abcdef


Why it copies 1st six characters to Ist and IInd 2D arrays. How to restrict so that I copy 1st six characters to 0th 2D array alone.


I am restricted to use only "sscanf" function. Help me out to solve
this.


Thanks & Regards
Sathish Kumar
Sep 5 '07 #1
2 1705
gpraghuram
1,275 Expert 1GB
Hi All

I want to copy a stream of data to 2D array. I wrote the following
program which was not working as i expected.

Code


#include <stdio.h>


char a[3][6];
char b[50]={"abcdefghijklmnopqrst"};
main()
{
int i=0;


for(i=0;i<3;i++)
sscanf(b, "%6c", a[i]);


for(i=0;i<3;i++)
printf("%s\n", a[i]);



}


Output

abcdefabcdefabcdef
abcdefabcdef
abcdef


But I want to have output like this


abcdef
abcdef
abcdef


Why it copies 1st six characters to Ist and IInd 2D arrays. How to restrict so that I copy 1st six characters to 0th 2D array alone.


I am restricted to use only "sscanf" function. Help me out to solve
this.


Thanks & Regards
Sathish Kumar
Hi,
Check this
Expand|Select|Wrap|Line Numbers
  1. char a[3][6]; 
  2. //change this to char a[3][8]; as you need 6 characters u have to allocate atleast 8 for second dimension
  3.  
This will solve the purpose.

Raghuram
Sep 5 '07 #2
HI Raghu

Thank you for your reply. The solution got worked. I did not give enough space for '\0' character.

Thanks & Regards
Sathish Kumar
Sep 5 '07 #3

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
8
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
7
by: lgbjr | last post by:
Hello All, I¡¯m using a context menu associated with some pictureboxes to provide copy/paste functionality. Copying the image to the clipboard was easy. But pasting an image from the clipboard...
0
by: NetronProject | last post by:
My copy/paste operation results in a MemoryStream full of '\0' characters on the Clipboard. Maybe my approach to copy/paste is wrong when handling generic types. All entries in the CollectionBase...
6
by: Julien | last post by:
Hello, I have some files located in a file server and managed by a SQL database from a web based interface using ASP + VBSCRIPT technology. I need to automatically copy those files to a web...
0
by: jcvoon | last post by:
Hi: I use the following code to copy the contents of the XMLDocument to byte array Dim xmlDoc as New XMLDocument Dim stream As New MemoryStream Dim buffers() As Byte ...
3
by: Arkady Frenkel | last post by:
Hi, guys! Is it possible to write byte array, I receive already from one stream to another, but not from first element, without copy part of array to new one. I mean I have byte array and need to...
3
by: Sir Psycho | last post by:
Hi, For some reason, when i step over this code, it returns the full byte stream im expecting from the server, however when I let it run with no intervention, it only seems to grab a small chunk...
4
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.