LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices

Reply
 
Search this Thread
Old 12-03-2012, 04:24 AM   #1
mithun_linux
LQ Newbie
 
Registered: Dec 2012
Posts: 7

Rep: Reputation: Disabled
Unhappy Help needed


Hi ppl i have a 20MB binary file .. i need a code to read the first 188 bytes of data and put that in a buffer and second 188 bytes of data into second buffer and so on.. i have to print first 188 byte and second 188 bytes and so on .. please help me out
 
Old 12-03-2012, 04:43 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,647

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
what language do you prefer? what have you tried so far?
 
1 members found this post helpful.
Old 12-03-2012, 04:53 AM   #3
mithun_linux
LQ Newbie
 
Registered: Dec 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
like i prefer C language .. the thing which i have tried so far is ... its getting printed but i dont know to find the EOF in my file..


#define __KERNEL_SYSCALLS__
#include <linux/version.h>
#include <linux/syscalls.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/smp_lock.h>
#include <linux/sched.h>
#include <linux/unistd.h>
#include <linux/string.h>
#include <linux/file.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
#include <asm/errno.h>
#include <asm/io.h>
static void read_file(char *filename)
{
//int fd;
struct file *fd;
loff_t pos =0;
unsigned char buf[1514];
int packets =0;

mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);

fd = filp_open("file name with path", O_RDONLY, 0);
if (fd >= 0) {
printk(KERN_INFO);
while (vfs_read(fd, buf,1316,&pos) == 1316)
{
int i;
printk("Packets %d\n",packets);
printk("###########\n");
for(i=0;i<188;i++)
{
printk("%02x ", buf[i]);
if(!(i % 20)) printk("\n");


}
printk("\n");
printk("\n");
pos += 188;
if(packets++ == 7800)
break;

}
filp_close(fd,NULL);
printk("\n");
}
else
printk("Error \n");

set_fs(old_fs);
}

static int __init init(void)
{
printk("init\n");
//read_file("/etc/shadow");
read_file("file name with path");
return 0;
}

static void __exit exit(void)
{ }

MODULE_LICENSE("GPL");
module_init(init);
module_exit(exit);
 
Old 12-03-2012, 05:22 AM   #4
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,647

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
I think vfs_read returns the number of bytes read. If it was less than the required (or 0) it means EOF
 
1 members found this post helpful.
Old 12-04-2012, 04:27 AM   #5
mithun_linux
LQ Newbie
 
Registered: Dec 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
The file which i am having is an .ts extension file i dont know to print the data because of unknown file format i am getting some junk data when i run the code can u please help me in this ppl??

#include <stdio.h>

main()
{
FILE *file;
char *buffer;
unsigned long fileLen;

file = fopen("kdg_20MB_wrappable.ts", "rb");
if (!file)
{
fprintf(stderr, "can't open file %s", "kdg_20MB_wrappable.ts");
exit(1);
}

fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);

buffer=(char *)malloc(fileLen+1);

if (!buffer)
{
fprintf(stderr, "Memory error!");
fclose(file);
exit(1);
}

fread(buffer, fileLen, 188, file);
fclose(file);

printf ("value is %s \n", &buffer);
dump_buffer(&buffer, fileLen);
//free(buffer);
}

dump_buffer(void *buffer, int buffer_size)
{
int i;

for(i = 0;i < buffer_size;++i)
printf("%c", ((char *)buffer)[i]);
}
 
Old 12-04-2012, 10:01 AM   #6
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,647

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
I would gladly help you just I do not know how. What do you really want to achieve?
you can use the command file <filename> to identify the type of the file, .ts is probably QT transport stream: http://filext.com/file-extension/TS, but I'm not really sure
You would need special software to handle such files.

Last edited by pan64; 12-04-2012 at 10:46 AM. Reason: typo
 
1 members found this post helpful.
Old 12-04-2012, 10:22 AM   #7
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 761
Blog Entries: 2

Rep: Reputation: 197Reputation: 197
Why all the kernel stuff? Why not open() and read() in userspace?
 
1 members found this post helpful.
Old 12-27-2012, 03:15 AM   #8
mithun_linux
LQ Newbie
 
Registered: Dec 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
But these transport stream file ".ts" is not getting opened in the user space with the functions
open() / fopen() / fread()... im confused
 
Old 12-27-2012, 04:38 AM   #9
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,647

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
use perror to print error messages:
http://www.java-samples.com/showtuto...tutorialid=597
 
1 members found this post helpful.
Old 02-28-2013, 09:25 AM   #10
vsselvaraj
LQ Newbie
 
Registered: Feb 2013
Posts: 5

Rep: Reputation: Disabled
Getting error.. :(

Same program i executed .. Please help me to solve the following errors .. I am new to kernal level file operations...


gcc -I/usr/src/linux-headers-3.2.0-23-generic/arch/x86/include -I/usr/src/linux-headers-3.2.0-23-generic/include/ testfile1.cpp
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:67:0,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/types.h:13:2: warning: #warning "Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders" [-Wcpp]
In file included from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/bitops.h:455:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/bitops.h:22,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:52,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/asm-generic/bitops/fls64.h:33:2: error: #error BITS_PER_LONG not 32 or 64
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:52:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/bitops.h:164:2: error: #error BITS_PER_LONG not 32 or 64
In file included from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/processor.h:26:0,
from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/thread_info.h:22,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:53,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/cpumask.h:148:37: error: "and" may not appear in macro parameter list
In file included from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/div64.h:57:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/math64.h:5,
from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/processor.h:29,
from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/thread_info.h:22,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:53,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/asm-generic/div64.h:54:3: error: #error do_div() does not yet support the C64
In file included from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/thread_info.h:22:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/thread_info.h:53,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/preempt.h:9,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/spinlock.h:50,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:38,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/processor.h:157:0: warning: "cache_line_size" redefined [enabled by default]
/usr/src/linux-headers-3.2.0-23-generic/include/linux/cache.h:64:0: note: this is the location of the previous definition
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/key.h:21:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:76,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/rcupdate.h:212:2: error: #error "Unknown RCU implementation specified to kernel configuration"
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/irq.h:20:0,
from /usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/hardirq.h:5,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/hardirq.h:7,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/ftrace_event.h:7,
from /usr/src/linux-headers-3.2.0-23-generic/include/trace/syscall.h:6,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/syscalls.h:77,
from testfile1.cpp:3:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/gfp.h:214:2: error: #error ZONES_SHIFT too large to create GFP_ZONE_TABLE integer
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/ktime.h:25:0,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/timer.h:5,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/workqueue.h:8,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/kmod.h:26,
from /usr/src/linux-headers-3.2.0-23-generic/include/linux/module.h:13,
from testfile1.cpp:4:
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
/usr/src/linux-headers-3.2.0-23-generic/include/linux/jiffies.h:43:42: error: division by zero in #if
In file included from /usr/src/linux-headers-3.2.0-23-generic/include/linux/module.h:22:0,
from testfile1.cpp:4:
/usr/src/linux-headers-3.2.0-23-generic/arch/x86/include/asm/module.h:59:2: error: #error unknown processor family
testfile1.cpp:6:28: fatal error: linux/smp_lock.h: No such file or directory
compilation terminated.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed smartgupta Linux - Networking 1 10-20-2008 10:09 AM
Help needed for make file problem : make: *** No rule to make target `flash', needed rameshsatyavaram Linux - Newbie 3 09-11-2008 02:13 AM
Help Needed. Andy12 Linux - General 9 09-25-2007 10:21 PM
XGL needed for Beryl is conflicting with OpenGL needed for Cedega chunkeydelight Linux - Desktop 2 07-18-2007 05:50 AM
Errors installing Q, which is needed for Lex, which is needed for PHP Virtuality Linux - Software 1 05-29-2007 04:47 PM


All times are GMT -5. The time now is 03:25 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration